Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First and last day of last month in Redshift

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?

like image 520
Nithin Chandy Avatar asked Aug 30 '16 19:08

Nithin Chandy


2 Answers

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'
like image 200
Gordon Linoff Avatar answered Sep 18 '22 02:09

Gordon Linoff


Or for last day in moth you can use a LAST_DAY Function. :) http://docs.aws.amazon.com/redshift/latest/dg/r_LAST_DAY.html

select last_day(current_date)

like image 23
Kirill Kuzikov Avatar answered Sep 18 '22 02:09

Kirill Kuzikov