简体   繁体   English

CAST(DateColumn AS DATE)和DATEADD(DD,DATEDIFF(DD,0,DateColumn()),0)之间的差异

[英]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) ? 我想知道与仅Cast(DateColumn as Date)相比,上述DateAdd和DateDiff的组合(广泛用于剥离时间)之间有什么区别?

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 ModifiedOn列是DateTime类型

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. SQL Server在2008版中引入了date数据类型。在以前的版本中,没有“便捷”的方法从datetime值中删除时间部分。 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. 解决方案是从某个标准时间点(例如“ 0”)计算天数,然后将这些天数相加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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