比较 DATETIME 和 DATE 忽略时间部分
我有两个表,其中列 [date]
的类型为 DATETIME2(0)
。
我必须仅通过日期部分(日+月+年)来比较两个记录,丢弃时间部分(小时+分钟+秒)。
我怎样才能做到这一点?
I have two tables where column [date]
is type of DATETIME2(0)
.
I have to compare two records only by theirs Date parts (day+month+year), discarding Time parts (hours+minutes+seconds).
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 SQL Server 2008 中新的
DATE
数据类型的CAST
来仅比较日期部分:Use the
CAST
to the newDATE
data type in SQL Server 2008 to compare just the date portion:Marc 的答案中的一个小缺点是两个日期字段都已进行类型转换,这意味着您将无法利用任何索引。
因此,如果需要编写一个可以从日期字段索引中受益的查询,那么以下(相当复杂的)方法是必要的。
(DF1 >= CAST(DF2 AS DATE)) AND (DF1 < DATEADD(dd, 1, CAST(DF2 AS DATE)))
PS:另一种仅提取日期的方法(在旧版本的 SQL Server 中)是使用日期内部表示方式的技巧。
CAST(FLOOR(CAST(DF2 AS FLOAT)) AS DATETIME)
A small drawback in Marc's answer is that both datefields have been typecast, meaning you'll be unable to leverage any indexes.
So, if there is a need to write a query that can benefit from an index on a date field, then the following (rather convoluted) approach is necessary.
(DF1 >= CAST(DF2 AS DATE)) AND (DF1 < DATEADD(dd, 1, CAST(DF2 AS DATE)))
PS: Another means of extracting the date only (in older versions of SQL Server) is to use a trick of how the date is represented internally.
CAST(FLOOR(CAST(DF2 AS FLOAT)) AS DATETIME)
尽管我投票赞成标记为正确的答案。我想为那些偶然发现这一点的人谈谈一些事情。
一般来说,如果您仅专门针对日期值进行过滤。 Microsoft 建议使用语言中性格式
ymd
或ymd
。使用上述方法进行日期比较很简单。考虑以下人为的示例。
在理想情况下,应该避免对过滤列执行任何操作,因为这会阻止 SQL Server 有效地使用索引。也就是说,如果您存储的数据仅与日期而不是时间有关,请考虑存储为
DATETIME
,并以午夜为时间。因为:因此,假设您只关心日期,并按此存储数据。上面的查询可以简化为:
Though I upvoted the answer marked as correct. I wanted to touch on a few things for anyone stumbling upon this.
In general, if you're filtering specifically on Date values alone. Microsoft recommends using the language neutral format of
ymd
ory-m-d
.To do a date comparison using the aforementioned approach is simple. Consider the following, contrived example.
In a perfect world, performing any manipulation to the filtered column should be avoided because this can prevent SQL Server from using indexes efficiently. That said, if the data you're storing is only ever concerned with the date and not time, consider storing as
DATETIME
with midnight as the time. Because:Thus, assuming you are only concerned with date, and store your data as such. The above query can be simplified to:
您可以尝试这个,
我通过以下代码针对 MS SQL 2014 进行了测试
You can try this one
I test that for MS SQL 2014 by following code
您可以使用
DateDiff
并按天进行比较。这意味着
@date2 > @date1
例如:
结果为:1
You may use
DateDiff
and compare by day.It means
@date2 > @date1
For example :
has the result : 1
用于比较两个日期,例如 MM/DD/YYYY 到 MM/DD/YYYY 。
请记住,字段的第一件事列类型必须是日期时间。
示例:columnName: payment_date dataType:DateTime。
之后您可以轻松比较它。
查询是:
非常简单......
测试了一下......
For Compare two date like MM/DD/YYYY to MM/DD/YYYY .
Remember First thing column type of Field must be dateTime.
Example : columnName : payment_date dataType : DateTime .
after that you can easily compare it.
Query is :
It very simple ......
It tested it.....