abs786123 abs786123 - 22 days ago 6
SQL Question

Difference between CAST(DateColumn AS DATE) and DATEADD(DD, DATEDIFF(DD, 0, DateColumn()), 0)

I would like to know the difference between

SELECT CAST(DateColumn AS DATE)


and

SELECT DATEADD(DD, DATEDIFF(DD, 0, DateColumn()), 0)


I have calculation which is long winded that looks like this:

sum(case when DATEADD(dd, DATEDIFF(dd, 0,ModifiedOn), 0) between DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) AND DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) -7 THEN 1 ELSE 0 END) [0-7 Days]


I would like to know what the different are between the above combination of DateAdd and DateDiff (which is widely used to strip off the time) compare to just
Cast(DateColumn as Date)
?

Would there be a difference? for me it looks the same but just wanted to be sure they both do the same thing so going forward I can implement the latter.

The ModifiedOn Column is a DateTime type

Thank you

Answer

They both do the same thing.

SQL Server introduced the date data type in version 2008. In previous versions, there was no "convenient" way to remove the time component from a datetime value. The solution was to calculate the number of days from some canonical point in time (say, "0") and then add those number of days back.