简体   繁体   English

如何将日期时间与SQL Server中的日期进行比较

[英]How to compare datetime with only date in SQL Server

Select * from [User] U
where  U.DateCreated = '2014-02-07'     

but in the database the user was created on 2014-02-07 12:30:47.220 and when I only put '2014-02-07' 但是在数据库中,用户是在2014-02-07 12:30:47.220 ,当时我只把'2014-02-07'

It does not show any data 它不显示任何数据

DON'T be tempted to do things like this: 不要试图做这样的事情:

Select * from [User] U where convert(varchar(10),U.DateCreated, 120) = '2014-02-07'

This is a better way: 这是一种更好的方法:

Select * from [User] U 
where U.DateCreated >= '2014-02-07' and U.DateCreated < dateadd(day,1,'2014-02-07')

see: Sargable (the page was removed from Wikipedia) 看: Sargable (该页面已从Wikipedia中删除)

EDIT + There are 2 fundamental reasons for avoiding use of functions on data in the where clause (or in join conditions). 编辑+避免在where子句(或连接条件)中使用数据上的函数有两个基本原因。

  1. In most cases using a function on data to filter or join removes the ability of the optimizer to access an index on that field, hence making the query slower (or more "costly") 在大多数情况下,使用数据函数来过滤或连接会删除优化器访问该字段上的索引的能力,从而使查询更慢(或更“昂贵”)
  2. The other is, for every row of data involved there is at least one calculation being performed. 另一种是,对于涉及的每行数据,至少执行一次计算。 That could be adding hundreds, thousands or many millions of calculations to the query so that we can compare to a single criteria like 2014-02-07 . 这可能会为查询添加数百,数千或数百万计算,以便我们可以比较像2014-02-07这样的单一标准。 It is far more efficient to alter the criteria to suit the data instead. 改变标准以适应数据的效率要高得多。

"Amending the criteria to suit the data" is my way of describing "use SARGABLE predicates" “修改标准以适应数据”是我描述“使用SARGABLE谓词”的方式


And do not use between either. 并且不要在两者之间使用。

the best practice with date and time ranges is to avoid BETWEEN and to always use the form: 日期和时间范围的最佳做法是避免BETWEEN并始终使用表单:

WHERE col >= '20120101' AND col < '20120201' This form works with all types and all precisions, regardless of whether the time part is applicable. WHERE col> ='20120101'AND col <'20120201'此表单适用于所有类型和所有精度,无论时间部分是否适用。

http://sqlmag.com/t-sql/t-sql-best-practices-part-2 (Itzik Ben-Gan) http://sqlmag.com/t-sql/t-sql-best-practices-part-2(Itzik Ben-Gan)

If you are on SQL Server 2008 or later you can use the date datatype: 如果您使用的是SQL Server 2008或更高版本,则可以使用date数据类型:

SELECT *
FROM [User] U
WHERE CAST(U.DateCreated as DATE) = '2014-02-07'

It should be noted that if date column is indexed then this will still utilise the index and is SARGable. 应该注意的是,如果对日期列进行索引,那么它仍将使用索引并且是SARGable。 This is a special case for dates and datetimes. 这是日期和日期时间的特例。

在此输入图像描述

You can see that SQL Server actually turns this into a > and < clause: 您可以看到SQL Server实际上将其转换为>和<子句:

在此输入图像描述

I've just tried this on a large table, with a secondary index on the date column as per @kobik's comments and the index is still used, this is not the case for the examples that use BETWEEN or >= and <: 我刚刚在一个大表上尝试了这个,根据@ kobik的注释在日期列上有一个二级索引,并且仍然使用索引,对于使用BETWEEN或> =和<:的示例不是这种情况。

SELECT *
FROM [User] U
WHERE CAST(U.DateCreated as DATE) = '2016-07-05'

显示二级索引的索引使用情况

According to your query Select * from [User] U where U.DateCreated = '2014-02-07' 根据您的查询Select * from [User] U where U.DateCreated = '2014-02-07'

SQL Server is comparing exact date and time ie (comparing 2014-02-07 12:30:47.220 with 2014-02-07 00:00:00.000 for equality). SQL Server正在比较确切的日期和时间即(比较2014-02-07 12:30:47.2202014-02-07 00:00:00.000相等)。 that's why result of comparison is false 这就是为什么比较的结果是错误的

Therefore, While comparing dates you need to consider time also. 因此,在比较日期时,您还需要考虑时间。 You can use 您可以使用
Select * from [User] U where U.DateCreated BETWEEN '2014-02-07' AND '2014-02-08' . Select * from [User] U where U.DateCreated BETWEEN '2014-02-07' AND '2014-02-08'

Of-course this is an old thread but to make it complete. 当然,这是一个旧线程,但要完成它。

From SQL 2008 you can use DATE datatype so you can simply do: 从SQL 2008开始,您可以使用DATE数据类型,这样您就可以执行以下操作:

SELECT CONVERT(DATE,GETDATE())

OR

Select * from [User] U
where  CONVERT(DATE,U.DateCreated) = '2014-02-07' 

You can use LIKE statement instead of = . 您可以使用LIKE语句而不是= But to do this with DateStamp you need to CONVERT it first to VARCHAR: 但要使用DateStamp执行此操作,您需要先将其CONVERT为VARCHAR:

SELECT * 
FROM [User] U
WHERE CONVERT(VARCHAR, U.DateCreated, 120) LIKE '2014-02-07%'

Please try this. 请试试这个。 This query can be used for date comparison 此查询可用于日期比较

select * from [User] U where convert(varchar(10),U.DateCreated, 120) = '2014-02-07'

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

相关问题 仅比较2个datetime列中的DATE-SQL - Compare only DATE from 2 datetime columns - sql 如何仅比较sql server 2008中的datetime字段的年份? - How to compare only year from a datetime field in sql server 2008? 如何在SQL Server中比较日期时间? - How to compare datetime in SQL Server? SQL Server,HQL:如何将SQL Server datetime列字段与日期进行比较 - SQL Server, HQL: How to compare SQL server datetime column field to date 如何仅从SQL Server DateTime数据类型返回带格式的日期 - How to return only the Date with formate from a SQL Server DateTime datatype 如何只将Year作为SQL Server中日期/日期时间的一部分 - How to only get Year as part of date / datetime in SQL Server 如何仅从 SQL 服务器 DateTime 数据类型返回日期 - How to return only the Date from a SQL Server DateTime datatype 如何在C#中的控制台应用程序中将SQL Server 2008数据库中的日期(仅)与系统日期(仅)进行比较 - How to compare date(only) from SQL Server 2008 database to the system date(only) in console application in C# 如何只存储日期时间的日期部分。 今天属性到SQL Server表的datetime列? - How can I store only the date portion of the datetime. Today property to a SQL Server table datetime column? 如何比较SQL Server中4个日期时间列之间的日期时间范围 - How to compare datetime range between 4 datetime columns in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM