Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current month and Last month in Redshift

I am wondering if I can get last month and current month in Redshift? I looked at functions at http://docs.aws.amazon.com/redshift/latest/dg/r_CURRENT_DATE_TIME_functions.html

For current month, I guess I can do the following:

LEFT(CURRENT_DATE, 7)

For previous month, I came up with the following:

LEFT(ADD_MONTHS(CURRENT_DATE, -1), 7)

But I am wondering if there is a simpler way of doing these?

like image 649
kee Avatar asked Dec 02 '14 01:12

kee


People also ask

How do I get the last day of the month in Redshift?

LAST_DAY returns the date of the last day of the month that contains date. The return type is always DATE, regardless of the data type of the date argument.

How do I get the current date in Redshift?

CURRENT_DATE returns a date in the current session time zone (UTC by default) in the default format: YYYY-MM-DD. CURRENT_DATE returns the start date for the current transaction, not for the start of the current statement.

How do I get the current date and timestamp in Redshift?

GETDATE returns the current date and time in the current session time zone (UTC by default). It returns the start date or time of the current statement, even when it is within a transaction block.

How do you get the last day of the week in Redshift?

Redshift offers a DATE_TRUNC('week', datestamp) function. Given any datestamp value, either a date or datetime, it gives back the date of the preceding Sunday.


1 Answers

SELECT EXTRACT(month FROM CURRENT_DATE);

SELECT EXTRACT(month FROM CURRENT_DATE - '1 month'::interval);
like image 120
Kouber Saparev Avatar answered Sep 27 '22 03:09

Kouber Saparev