SQL选择startdate是今天的日期

标签 sql sql-server sql-server-2008 tsql datetime

我正在尝试编写一个查询,其中子句是员工的开始日期是今天的日期。

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/

相关文章:

java - 编写外键关联查询

python - PANDAS - 为多个列正确执行嵌套分组(几列构成唯一标识符)

sql - MySQL 语句结合了连接和计数?

sql-server - 如何对 SQL Server 数据库进行版本控制?

sql-server - SQL Server : Is there a performance cost to computed columns?

sql - 别名在 sql server 2008 r2 上不起作用

sql - 在 Entity Framework 中使用正则表达式进行搜索

sql - 不用聚合函数求最大值

sql-server - 如何优化SQL Server查询的where子句

sql - 如何跟踪列更改其值的次数?