[英]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子句(或连接条件)中使用数据上的函数有两个基本原因。
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.220
与2014-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.