SQL Getdate(), -1 天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34738836/
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
Getdate(), -1 day
提问by N2hvits
I do not understand why, but somehow this query doesn't work. I want to take system date -1 day where the sysdate is smaller by 1 day then current date.
我不明白为什么,但不知何故此查询不起作用。我想取系统日期 -1 天,其中 sysdate 比当前日期小 1 天。
WHERE
a.SEND_Date >= dateadd(DD,-1,(CAST(getdate() as date) as datetime))
回答by Yahel
The CAST depends on what kind of date type you need. If you need only to compare dates you can use only:
CAST 取决于您需要什么样的日期类型。如果您只需要比较日期,则只能使用:
dateadd(DD, -1, cast(getdate() as date))
If you need to compare with date time you can use:
如果您需要与日期时间进行比较,您可以使用:
dateadd(DD,-1,getdate())
That wil give you datetime like this: 2016-01-11 10:43:57.443
这会给你这样的日期时间: 2016-01-11 10:43:57.443
回答by FSciacca
In T-SQL (sqlserver) you can simply do :
在 T-SQL (sqlserver) 中,您可以简单地执行以下操作:
getDate()-1
The function substracts (days) as standard.
函数减去(天)作为标准。
回答by Ivan Ivanov
Or you can try this without making it any more difficult?
或者你可以试试这个而不让它变得更困难?
CAST(GETDATE()-1 as date )
回答by Aravind
Please Don't follow any as this date will come issue on 01/05/2020 will print as 00/05/2020. So kindly use this below for the fix
请不要遵循任何内容,因为此日期将于 01/05/2020 发布,打印为 00/05/2020。所以请在下面使用它进行修复
select CONVERT(varchar, DATEADD(DAY, -1, convert(Nvarchar, GETDATE(),112)),112)
回答by user3806549
The checked answer still has time (00:00:00), in my version anyway. To get DATE only use: Select Convert(date,dateadd(day, -1, getdate()))
无论如何,在我的版本中,检查的答案仍然有时间(00:00:00)。要仅获取 DATE,请使用:选择 Convert(date,dateadd(day, -1, getdate()))
Both have same weight, 0.001 seconds
两者具有相同的权重,0.001 秒
回答by rf0806
There's just one CAST missing:
只缺少一个 CAST:
dateadd(DD,-1,(CAST(getdate() as date) as datetime))
two times "as" (as date + as datetime) but only one time "CAST" => something wrong - should be:
两次“作为”(作为日期+作为日期时间)但只有一次“CAST”=>出了点问题 - 应该是:
dateadd(DD,-1,CAST(CAST(getdate() as date) as datetime))