Re: incrementing and decrementing dates by day increments programmatically

Поиск
Список
Период
Сортировка
От Alvaro Herrera
Тема Re: incrementing and decrementing dates by day increments programmatically
Дата
Msg-id 20031026203329.GC12063@dcc.uchile.cl
обсуждение исходный текст
Ответ на incrementing and decrementing dates by day increments programmatically  (nzanella@cs.mun.ca (Neil Zanella))
Список 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 по дате отправления:

Предыдущее
От: Allen Landsidel
Дата:
Сообщение: Question regarding Perl, DBI, and fork()
Следующее
От: Stephan Szabo
Дата:
Сообщение: Re: incrementing and decrementing dates by day increments