Get timestamp of one month ago in PostgreSQL

Question was answered by a friend in IRC:

'now'::timestamp - '1 month'::interval

Having the timestamp return 00:00:00 wasn't terrible important, so this works for my intentions.


select date_trunc('day', NOW() - interval '1 month')

This query will return date one month ago from now and round time to 00:00:00.


When you need to query for the data of previous month, then you need to query for the respective date column having month values as (current_month-1).

SELECT * 
FROM {table_name}
WHERE {column_name} >= date_trunc('month', current_date-interval '1' month)
    AND {column_name} < date_trunc('month', current_date)

The first condition of where clause will search the date greater than the first day (00:00:00 Day 1 of Previous Month)of previous month and second clause will search for the date less than the first day of current month(00:00:00 Day 1 of Current Month). This will includes all the results where date lying in previous month.

Tags:

Sql

Postgresql