公告
财富商城
积分规则
提问
发文
2019-03-04 19:48发布
欢心
how to write query to get today's date data in SQL server ?
SQL server
select * from tbl_name where date = <Todays_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
CAST
GETDATE()
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.
DATEDIFF()
Cast(x to Date)
SELECT * FROM tbl_name WHERE date >= cast(getdate() as date) and date < cast(getdate()+1 as date)
The correct answer will depend of the exact type of your datecolumn. Assuming it is of type Date :
datecolumn
Date
select * from tbl_name where datecolumn = cast(getdate() as Date)
If it is DateTime:
DateTime
select * from tbl_name where cast(datecolumn as Date) = cast(getdate() as Date)
最多设置5个标签!
for
CAST
see http://msdn.microsoft.com/en-us/library/ms187928.aspx and forGETDATE()
see https://msdn.microsoft.com/en-IN/library/ms188383.aspxSeems 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 asCast(x to Date)
is.The correct answer will depend of the exact type of your
datecolumn
. Assuming it is of typeDate
:If it is
DateTime
: