SQL 从日期时间减去一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15631977/
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
Subtract one day from datetime
提问by James
I have a query to fetch date diff between 2 datetime as :
我有一个查询来获取 2 个日期时间之间的日期差异:
SELECT DATEDIFF(DAY, @CreatedDate , GETDATE())
Ex :
SELECT DATEDIFF(DAY, '2013-03-13 00:00:00.000' , GETDATE())
I need to have a query work like this which will subtract a day from created day:
我需要有一个这样的查询工作,它将从创建日中减去一天:
SELECT DATEDIFF(DAY, **@CreatedDate- 1** , GETDATE())
回答by yogi
Try this
尝试这个
SELECT DATEDIFF(DAY, DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())
OR
或者
SELECT DATEDIFF(DAY, DATEADD(day, -1, @CreatedDate), GETDATE())
回答by Chris
I am not certain about what precisely you are trying to do, but I think this SQL function will help you:
我不确定你到底要做什么,但我认为这个 SQL 函数会帮助你:
SELECT DATEADD(day,-1,'2013-04-01 16:25:00.250')
The above will give you 2013-03-31 16:25:00.250
.
以上就给你2013-03-31 16:25:00.250
。
It takes you back exactly one day and works on any standard date-time or date format.
它带您返回整整一天,并适用于任何标准日期时间或日期格式。
Try running this command and see if it gives you what you are looking for:
尝试运行此命令,看看它是否为您提供了您要查找的内容:
SELECT DATEADD(day,-1,@CreatedDate)
回答by FoxDeploy
To simply subtract one day from todays date:
简单地从今天的日期减去一天:
Select DATEADD(day,-1,GETDATE())
(original post used -7 and was incorrect)
(原始帖子使用 -7 并且不正确)
回答by Philip Rego
Apparently you can subtract the number of days you want from a datetime.
显然,您可以从日期时间中减去您想要的天数。
SELECT GETDATE() - 1
2016-12-25 15:24:50.403
回答by samithagun
This should work.
这应该有效。
select DATEADD(day, -1, convert(date, GETDATE()))
回答by meekon5
To be honest I just use:
老实说,我只是使用:
select convert(nvarchar(max), GETDATE(), 112)
which gives YYYYMMDD
and minus one from it.
它给出YYYYMMDD
和减去一个。
Or more correctly
或者更正确
select convert(nvarchar(max), GETDATE(), 112) - 1
for yesterdays date.
对于昨天的日期。
Replace Getdate()
with your value OrderDate
替换Getdate()
为您的值OrderDate
select convert(nvarchar (max),OrderDate,112)-1 AS SubtractDate FROM Orders
should do it.
应该这样做。
回答by Daniel Imms
SELECT DATEDIFF (
DAY,
DATEDIFF(DAY, @CreatedDate, -1),
GETDATE())
回答by Vijay Singh Rana
Try this, may this will help you
试试这个,这可能会帮助你
SELECT DATEDIFF(DAY, DATEADD(DAY,-1,'2013-03-13 00:00:00.000') , GETDATE())
回答by chintan
You can try this.
你可以试试这个。
Timestamp=2008-11-11 13:23:44.657;
时间戳 = 2008-11-11 13:23:44.657;
SELECT DATE_SUB(OrderDate,INTERVAL 1 DAY) AS SubtractDate FROM Orders
output :2008-11-10 13:23:44.657
输出:2008-11-10 13:23:44.657
I hope, it will help to solve your problem.
我希望,这将有助于解决您的问题。