如何在sql server中获取当前/今天的日期数据

时间:2015-03-05 08:03:11

标签: sql sql-server current-time

如何编写查询以获取SQL server中的今天日期数据?

select * from tbl_name where date = <Todays_date>

4 个答案:

答案 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.aspxGETDATE()查看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();

现在您可以获取今天插入的行