SQL 如何在sql server中获取当前/今天的日期数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28872787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:23:08  来源:igfitidea点击:

how to get current/Todays date data in sql server

sqlsql-servercurrent-time

提问by krishna mohan

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

如何编写查询以获取今天的日期数据SQL server

select * from tbl_name where date = <Todays_date>

回答by Mitch Wheat

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)

回答by t-clausen.dk

Seems Mitch Wheat's answer isn't sargable, although I am not sure this is true.

似乎 Mitch Wheat 的回答不是sargable,虽然我不确定这是真的。

Please note: a DATEDIFF()(or other calculation) on LHS is NOT Sargable, whereas as Cast(x to Date)is.

请注意:DATEDIFF()LHS 上的a (或其他计算)不是 Sargable,而原样Cast(x to Date)

SELECT * 
FROM tbl_name 
WHERE date >= cast(getdate() as date)
and date < cast(getdate()+1 as date)