Re: incrementing and decrementing dates by day increments programmatically - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: incrementing and decrementing dates by day increments programmatically
Date
Msg-id 20031026203329.GC12063@dcc.uchile.cl
Whole thread Raw
In response to incrementing and decrementing dates by day increments programmatically  (nzanella@cs.mun.ca (Neil Zanella))
List 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)

pgsql-general by date:

Previous
From: Allen Landsidel
Date:
Subject: Question regarding Perl, DBI, and fork()
Next
From: Stephan Szabo
Date:
Subject: Re: incrementing and decrementing dates by day increments