Re: Age function

From: Jon Sime <jsime(at)mediamatters(dot)org>
To: Andrus <kobruleht2(at)hot(dot)ee>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Age function
Date: 2007-05-14 20:10:01
Message-ID: 4648C219.8000805@mediamatters.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2007-05-14 20:24:01 Re: How to implement GOMONTH function
Previous Message Alexander Staubo 2007-05-14 20:04:49 Re: Age function