Subtract one day from datetime

后端 未结 9 1814
你的背包
你的背包 2020-12-08 08:55

I have a query to fetch date diff between 2 datetime as :

SELECT DATEDIFF(DAY, @CreatedDate , GETDATE())

Ex :

SELECT DATEDIFF(DAY, \'2013-03-13 00:00:00.00         


        
相关标签:
9条回答
  • 2020-12-08 09:33

    To be honest I just use:

    select convert(nvarchar(max), GETDATE(), 112)
    

    which gives YYYYMMDD and minus one from it.

    Or more correctly

    select convert(nvarchar(max), GETDATE(), 112) - 1 
    

    for yesterdays date.

    Replace Getdate() with your value OrderDate

    select convert(nvarchar (max),OrderDate,112)-1 AS SubtractDate FROM Orders
    

    should do it.

    0 讨论(0)
  • 2020-12-08 09:35

    To simply subtract one day from todays date:

    Select DATEADD(day,-1,GETDATE())
    

    (original post used -7 and was incorrect)

    0 讨论(0)
  • 2020-12-08 09:41

    I am not certain about what precisely you are trying to do, but I think this SQL function will help you:

    SELECT DATEADD(day,-1,'2013-04-01 16:25:00.250')
    

    The above will give you 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)
    
    0 讨论(0)
提交回复
热议问题