Re: generate_series with month intervals

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marcus Engene <mengpg(at)engene(dot)se>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: generate_series with month intervals
Date: 2007-01-11 19:35:59
Message-ID: 28853.1168544159@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Marcus Engene <mengpg(at)engene(dot)se> writes:
> I tried this but it didn't work:

> select date_trunc ('month', now())::date + interval s.a || ' months'
> from generate_series(0, 11) as s(a)

People keep trying that :-(. The "typename 'foo'" syntax is for a
*literal constant* only. Instead use multiplication, something like

select (date_trunc('month', now())::date + s.a * '1 month'::interval)::date
from generate_series(0, 11) as s(a)

You'll want the cast back to date as the last step here because
date+interval will give timestamp.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Patrick Earl 2007-01-11 19:37:26 Re: Checkpoint request failed on version 8.2.1.
Previous Message Jonathan Hedstrom 2007-01-11 19:32:26 Re: ERROR: invalid memory alloc request size, and others