Elad Mazor
Elad Mazor

Reputation: 41

Subtract month from date column

I would like to subtract one month from a date column in Amazon Redshift (a fork of PostgreSQL 8.0.2).

So for each date column in a table, it will add another column date_minus_a_month.

I tried this code

Select date,date::date -interval '1 month'
from table

and received an error:

Interval values with month or year parts are not supported.

Does anyone have a solution for this?

Upvotes: 4

Views: 8969

Answers (1)

ScottieB
ScottieB

Reputation: 4052

You can use datesub, although I just use dateadd for everything and use negative numbers.

eg

SELECT getdate() as right_now, dateadd(month, -1, getdate()) as last_month

Docs: https://docs.aws.amazon.com/redshift/latest/dg/r_DATEADD_function.html

Upvotes: 4

Related Questions