Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Select Records based on current date minus two days

I have an orders table which contains an order ID, order date and order description.

I want to run a select query which captures all orders that have been created in the last two days. so the current date minus two days. from the 14th December, I would want to select all orders where the order date is > 13th December. This needs to use a Get date function to pick up the current date and minus the days.

I have tried:

select * from orders where orderdate > getdate() - 2 

but this is not producing the correct results. Any idea's how to do this please?

like image 829
Emma Avatar asked Dec 14 '17 16:12

Emma


People also ask

How do I subtract 3 days from a date in SQL?

The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.

How do I subtract days from current date in SQL?

We can use DATEADD() function like below to Subtract days from DateTime in Sql Server. DATEADD() functions first parameter value can be day or dd or d all will return the same result.

How do I subtract two dates in SQL?

The DATEDIFF() function returns the difference between two dates.

Can you subtract in SQL Select?

The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is used to subtract the result set obtained by first SELECT query from the result set obtained by second SELECT query.


1 Answers

you should try to use dateadd function

select * from orders where orderdate > dateadd(dd,-1,cast(getdate() as date))

Now this may exactly what you need but then you need to understand that by casting to date we remove the time part and effectively going back to start of the day and a day behind it(-1) gives the start of yesterday.

like image 83
DhruvJoshi Avatar answered Dec 15 '22 01:12

DhruvJoshi