我正在尝试编写一个查询,其中的子句是员工的开始日期是今天的日期。
select * from tbl_employees
where Startdate = getdate()问题是开始日期是'2014-12-09 00:00:00.000‘,函数getdate返回的日期和时间类似于'2014-12-09 08:25:16.013’。
我怎么写一个只考虑日期的查询呢?
发布于 2014-12-09 21:33:58
您只需要日期部分。最简单的方法是:
select *
from tbl_employees
where cast(Startdate as date) = cast(getdate() as date);但是,如果要使用索引,最好不要在函数调用中包含该列。所以,这样更好:
where (StartDate >= cast(getdate() as date) and StartDate < cast(getdate() + 1 as date))发布于 2014-12-09 21:34:37
select * from tbl_employees
where CONVERT(VARCHAR(10),Startdate,110) = CONVERT(VARCHAR(10),GETDATE(),110)发布于 2014-12-09 21:35:07
你可以用来只比较日期部分而不是time..some内容,比如
IF CAST(DateField1 AS DATE) = CAST(DateField2 AS DATE)或
CONVERT(VARCHAR(10), GETDATE(), 112)https://stackoverflow.com/questions/27380132
复制相似问题