how to get current/Todays date data in sql server

2019-03-04 19:48发布

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

select * from tbl_name where date = <Todays_date>

3条回答
Viruses.
2楼-- · 2019-03-04 20:03
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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-04 20:05

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)
查看更多
男人必须洒脱
4楼-- · 2019-03-04 20:13

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)
查看更多
登录 后发表回答