how to get current/Todays date data in sql server

家住魔仙堡 提交于 2019-12-31 01:43:06

问题


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

select * from tbl_name where date = <Todays_date>

回答1:


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)



回答2:


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:


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)


来源:https://stackoverflow.com/questions/28872787/how-to-get-current-todays-date-data-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!