如何在 sql 服务器中获取 current/Todays 日期数据

how to get current/Todays date data in sql server

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

select * from tbl_name where date = <Todays_date>

正确答案将取决于您 datecolumn 的确切类型。假设它是类型 Date :

select * from tbl_name 
where datecolumn = cast(getdate() as Date)

如果是DateTime:

select * from tbl_name 
where cast(datecolumn as Date) = cast(getdate() as Date)
select * from tbl_name where date = cast(getdate() as Date)

对于 CAST 参见 http://msdn.microsoft.com/en-us/library/ms187928.aspx and for GETDATE() see https://msdn.microsoft.com/en-IN/library/ms188383.aspx

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

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

SELECT * 
FROM tbl_name 
WHERE date >= cast(getdate() as date)
and date < cast(getdate()+1 as date)
select * from tbl_name where date(column_name) = CURDATE();

现在您可以获得今天插入的行