Re: Age function

Поиск
Список
Период
Сортировка
От Jon Sime
Тема Re: Age function
Дата
Msg-id 4648C219.8000805@mediamatters.org
обсуждение исходный текст
Ответ на Age function  ("Andrus" <kobruleht2@hot.ee>)
Список pgsql-general
Andrus wrote:
> How to create function which returns persons age in years?
>
> Function parameters:
>
> ldDob - Day Of birth
> ldDate - Day where age is returned
>
>
> I tried
> CREATE OR REPLACE FUNCTION public.age(date, date, out integer) IMMUTABLE AS
> $_$
> SELECT floor(INT($2::text::integer-$1::text::integer)/10000);
> $_$ language sql

There's already an age(timestamp [, timestamp]) function available for this:

     select age('1912-06-23'::date);
or
     select age(now()::date, '1912-06-23'::date);

And if you want just the number of years, use date_part to extract just
that piece:

     select date_part('year', age(now()::date, '1912-06-23'::date));

Based on this and your other question about functions that followed, you
may want to read the Date and Time Functions section of the docs:

http://www.postgresql.org/docs/8.2/interactive/functions-datetime.html

-Jon

--
Senior Systems Developer
Media Matters for America
http://mediamatters.org/

В списке pgsql-general по дате отправления:

Предыдущее
От: "Alexander Staubo"
Дата:
Сообщение: Re: Age function
Следующее
От: Rich Shepard
Дата:
Сообщение: Re: How to implement GOMONTH function