title: SQL Server DateDiff between two dates returns inaccurate values
body: <p>I am trying to convert a string into <code>DateTimeOffset</code> (in SQL Server) through a ETL job. Basically, my string would look something like '2017-10-15' and I want this to be converted into a <code>DatetimeOffset</code> (from the current DB server). </p>
  
  <pre><code>SELECT
      SWITCHOFFSET(DATEADD(mi, DATEDIFF(MI, GETDATE(), GETUTCDATE()), CAST(@DateInString  + ' 00:00:00' AS DATETIMEOFFSET)), DATENAME(tzoffset, SYSDATETIMEOFFSET()))
  </code></pre>
  
  <p>I have been getting some weird issues with this statement as the final output would fall either +1 / -1 minute than the expected ones. This happens for at least every 10 records/million. I tried to nail down the issue and I could see the problem was with the <code>DATEDIFF()</code> method returning +/-1 minute.</p>
  
  <pre><code>SELECT DATEDIFF(MI, GETDATE(), GETUTCDATE()) 
  </code></pre>
  
  <p>This should exactly return -600 (since my DB server UTC is +10). However, it returns either -599 or 601 for few records. I execute them as a single select statement in my Stored Procedure and return it as a parameter. </p>
  
  <p>This is weird on how SQL could detect two different datetime values for <code>GETDATE()</code> and <code>GETUTCDATE()</code> on the same select statement. </p>
  
  <p>Is there a way to force SQL to get exactly same dates in those <code>DATEDIFF</code> parameters or am I missing something here? Thanks in advance</p>
  
  <p>I am using SQL Server 2014 (v12.0).</p>
  
  <p>Stored procedure:</p>
  
  <pre><code>CREATE PROCEDURE dbo.SPConvertDateTimeOffset
      @DateInString VARCHAR(10),
      @DateTimeOffset_Value DATETIMEOFFSET OUTPUT,
      @Datediff_Value INT OUTPUT
  AS 
  BEGIN 
      -- This line returns +/- 1
      SELECT @Datediff_Value = DATEDIFF(MI, GETDATE(), GETUTCDATE())  
  
      SELECT @DateTimeOffset_Value = SWITCHOFFSET(DATEADD(mi, @Datediff_Value, CAST(@DateInString  + ' 00:00:00' AS DATETIMEOFFSET)), DATENAME(tzoffset, SYSDATETIMEOFFSET()))
  END
  </code></pre>