Search code examples
sqlsql-server-2008t-sqldatediffdateadd

Why would someone use this?


I came across this line of code in a SQL Report I am trying to fix. Could someone tell me what is the purpose of this.

DATEADD(dd, - DATEDIFF(dd, d.TxnDateTime, 1), 1) As TxnDate

To me it seems like it would cancel itself out.


Solution

  • It's removing the TIME portion of the DATETIME without changing the data type. You can observe the different behavior here: SQL Fiddle

    Again, not sure why you'd need to preserve the DATETIME type while removing the time, unless it predates the DATE datatype.