use*_*463 4 sql t-sql sql-server datetime sql-server-2008
我正在尝试编写一个查询,其中子句是员工的开始日期是今天的日期。
select * from tbl_employees
where Startdate = getdate()
Run Code Online (Sandbox Code Playgroud)
问题是 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);
Run Code Online (Sandbox Code Playgroud)
但是,如果要使用索引,最好不要在函数调用中包含该列。所以,这是更好的:
where (StartDate >= cast(getdate() as date) and StartDate < cast(getdate() + 1 as date))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10926 次 |
| 最近记录: |