13

how to write query to get today's date data in SQL server ?

select * from tbl_name where date = <Todays_date>
4

4 回答 4

44

The correct answer will depend of the exact type of your datecolumn. Assuming it is of type Date :

select * from tbl_name 
where datecolumn = cast(getdate() as Date)

If it is DateTime:

select * from tbl_name 
where cast(datecolumn as Date) = cast(getdate() as Date)
于 2015-03-05T08:04:43.717 回答
6

似乎 Mitch Wheat 的回答不是sargable,虽然我不确定这是真的。

请注意:DATEDIFF()LHS 上的 a(或其他计算)不是 Sargable,而 as Cast(x to Date)is。

SELECT * 
FROM tbl_name 
WHERE date >= cast(getdate() as date)
and date < cast(getdate()+1 as date)
于 2015-03-05T08:23:38.427 回答
4
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 _

于 2015-03-05T08:05:07.447 回答
-3
select * from tbl_name where date(column_name) = CURDATE();

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

于 2020-08-21T09:19:34.283 回答