简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM