Subtract one day from datetime

SqlSql ServerDatetime

Sql Problem Overview


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.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())

Sql Solutions


Solution 1 - Sql

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())

Solution 2 - Sql

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)

Solution 3 - Sql

To simply subtract one day from todays date:

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

(original post used -7 and was incorrect)

Solution 4 - Sql

Apparently you can subtract the number of days you want from a datetime.

SELECT GETDATE() - 1

2016-12-25 15:24:50.403

Solution 5 - Sql

This should work.

select DATEADD(day, -1, convert(date, GETDATE()))

Solution 6 - Sql

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

Solution 7 - Sql

Try this, may this will help you

SELECT DATEDIFF(DAY, DATEADD(DAY,-1,'2013-03-13 00:00:00.000') , GETDATE())

Solution 8 - Sql

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.

Solution 9 - Sql

You can try this.

Timestamp=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

I hope, it will help to solve your problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - SqlyogiView Answer on Stackoverflow
Solution 2 - SqlChrisView Answer on Stackoverflow
Solution 3 - SqlFoxDeployView Answer on Stackoverflow
Solution 4 - SqlPhilip RegoView Answer on Stackoverflow
Solution 5 - SqlsamithagunView Answer on Stackoverflow
Solution 6 - SqlDaniel ImmsView Answer on Stackoverflow
Solution 7 - SqlVijay Singh RanaView Answer on Stackoverflow
Solution 8 - Sqlmeekon5View Answer on Stackoverflow
Solution 9 - SqlchintanView Answer on Stackoverflow