我正在尝试编写一个查询,其中子句是员工的开始日期是今天的日期。
select * from tbl_employees
where Startdate = getdate()
问题是 Startdate 是 '2014-12-09 00:00:00.000'
并且函数
getdate
返回日期和时间,如“2014-12-09 08:25:16.013”如何编写仅考虑日期的查询?
最佳答案
您只需要日期部分。简单的方法是:
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))
关于SQL选择startdate是今天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27380132/