how to write query to get today's date data in SQL server
?
select * from tbl_name where date = <Todays_date>
how to write query to get today's date data in SQL server
?
select * from tbl_name where date = <Todays_date>
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)
似乎 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)
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 _
select * from tbl_name where date(column_name) = CURDATE();
现在您可以获得今天插入的行