title: SQL Datediff in seconds with decimal places
body: <p>I am trying to extract the difference between two SQL DateTime values in seconds, with decimal places for some performance monitoring.  </p>
  
  <p>I have a table, "Pagelog" which has a "created" and "end" datetime.  In the past I have been able to do the following:</p>
  
  <pre><code>SELECT DATEDIFF(ms, pagelog_created, pagelog_end)/1000.00 as pl_duration FROM pagelog
  </code></pre>
  
  <p>However I have started getting the following error:</p>
  
  <pre><code>Msg 535, Level 16, State 0, Line 1
  The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
  </code></pre>
  
  <p>I have seen numerous responses to this error stating that I should use a less precise unit of measurement.  But this hardly helps when I need to distinguish between 2.1 seconds and 2.9 seconds, because DATEDIFF(s,..,..) will return INT results and lose the accuracy I need.</p>
  
  <p>I originally thought that this had been caused by a few values in my table having a huge range but running this:</p>
  
  <pre><code>SELECT DATEDIFF(s, pagelog_created, pagelog_end) FROM pagelog
  ORDER BY DATEDIFF(s, pagelog_created, pagelog_end) DESC
  </code></pre>
  
  <p>Returns a max value of 30837, which is 8.5 hours or 30,837,000 milliseconds, well within the range of a SQL INT as far as I know?</p>
  
  <p>Any help would be much appreciated, as far as I can tell I have two options:</p>
  
  <ul>
  <li>Somehow fix the problem with the data, finding the culprit values</li>
  <li>Find a different way of calculating the difference between the values</li>
  </ul>
  
  <p>Thanks!</p>