如何编写查询以获取SQL server
中的今天日期数据?
select * from tbl_name where date = <Todays_date>
答案 0 :(得分:11)
正确答案取决于datecolumn
的确切类型。假设它是Date
:
select * from tbl_name
where datecolumn = cast(getdate() as Date)
如果是DateTime
:
select * from tbl_name
where cast(datecolumn as Date) = cast(getdate() as Date)
答案 1 :(得分:3)
select * from tbl_name where date = cast(getdate() as Date)
代表CAST
查看http://msdn.microsoft.com/en-us/library/ms187928.aspx和GETDATE()
查看https://msdn.microsoft.com/en-IN/library/ms188383.aspx
答案 2 :(得分:3)
似乎Mitch Wheat的答案不是sargable,虽然我不确定这是真的。
请注意:LHS上的DATEDIFF()
(或其他计算)不可分,而Cast(x to Date)
则为{。}}。
SELECT *
FROM tbl_name
WHERE date >= cast(getdate() as date)
and date < cast(getdate()+1 as date)
答案 3 :(得分:-2)
select * from tbl_name where date(column_name) = CURDATE();
现在您可以获取今天插入的行