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 on the 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)
Please note: In SQL Server, a datediff()
(or other calculation) on a column is NOT Sargable, whereas as CAST(column AS Date)
is.
Seems Mitch Wheat's answer isn't sargable, although I am not sure this is true.
Please note: a DATEDIFF()
(or other calculation) on LHS is NOT Sargable, whereas 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)
for CAST
see http://msdn.microsoft.com/en-us/library/ms187928.aspx and for GETDATE()
see https://msdn.microsoft.com/en-IN/library/ms188383.aspx
It works for me. Please check it out.
SELECT * FROM tbl_name WHERE created_at = cast(Date(Now()) as Date);
select * from tbl_name where date(column_name) = CURDATE();
Now you can get today inserted rows