Search code examples
sqlpostgresqldate-arithmetic

Filtering data for the last 3 months using DATEADD() in SQL


I am trying to show data from the last three months so have used

SELECT my_date::date AS my_date, my_col
FROM my_table     
WHERE my_date >= DATEADD(MONTH, -3, GETDATE())
GROUP BY my_date
ORDER BY my_date DESC

But I get the following error

ERROR:  column "month" does not exist
LINE 10: WHERE process_date >= DATEADD(MONTH, -3, GETDATE())
                                       ^
SQL state: 42703
Character: 651

I am not sure why it doesn't like MONTH, I am trying to use DATEADD() as shown here https://www.w3schools.com/sql/func_sqlserver_dateadd.asp DATEADD(interval, number, date).

How should I be filtering data for the last 3 months and why does DATEADD() not seem to be working?


Solution

  • Thanks to @VvdL for their comment.

    PostgreSQL uses a different command. Changed to below and works.

    WHERE my_date >= current_date - INTERVAL '3 months'