First and last day of last month in Redshift

Nithin Chandy picture Nithin Chandy · Aug 30, 2016 · Viewed 22.3k times · Source

I want to find the first and last day of last month in Redshift. However, most of the postgresql features like date_trunc do not seem to work. How can I achieve that?

Answer

Gordon Linoff picture Gordon Linoff · Aug 30, 2016

In Redshift, you can use date_trunc() (See online documentation).

For the first day of last month:

select date_trunc('month', current_date) - interval '1 month'

Or:

select date_add(month, -1, date_trunc('month', current_date))

For the last day of last month:

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