Skip to content
Advertisement

How to go back two days from a date in sql?

I wrote an SQL query that allows me to get the sales of certain stores. My query runs every mornings and I would like to get the sales from 2 days ago at runtime.

For example if my query runs tomorrow morning, on 08/12, I would like to have the sales whose value in the column "GP_HEURECREATION" starts with “20200612”, to have all the sales of the whole day.

The GP_HEURECREATION column has a format like this: "20200612 00:00:00" and is of the DATE type.

I tried with NOW() and DATEADD() but I have 2018 values that stand out for example. How can I get the values only two days before the query is executed?

Advertisement

Answer

The syntax of your existing query suggests SQL Server. If you want records that belong to day -2, you can do:

If gp_heurecreation has no time component (in SQL Server, that’s a date datatype), this is simpler:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement