Re: incrementing and decrementing dates by day increments programmatically

From: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
To: Neil Zanella <nzanella(at)cs(dot)mun(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: incrementing and decrementing dates by day increments programmatically
Date: 2003-10-26 20:33:29
Message-ID: 20031026203329.GC12063@dcc.uchile.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Oct 25, 2003 at 09:35:35PM -0700, Neil Zanella wrote:
> Hello,
>
> I know that PostgreSQL, like most database management systems, has a
> function
> call called NOW() that returns the current date. Is there a way to
> return a datein PostgreSQL such that the output is in ISO 8601 format
> (Unix 'date -I' format)but such that the date is not "today"'s date
> but the date two days ago or five
> days ahead of now?

Certainly. Try the following:
SELECT now() + 5 * '1 day'::interval;

Or, more verbose,
SELECT now() + 5 * CAST('1 day' AS interval);

You can of course do
SELECT now() + CAST('5 day' AS interval);

But the two previous examples can be more easily constructed in an SQL o
PL/pgSQL function.

For the date -I format you can use something like
SELECT to_char(now() + 5 * '1 day'::interval, 'YYYY-MM-DD');

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"La tristeza es un muro entre dos jardines" (Khalil Gibran)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2003-10-26 21:31:46 Re: incrementing and decrementing dates by day increments
Previous Message Allen Landsidel 2003-10-26 20:04:29 Question regarding Perl, DBI, and fork()