Re: Age function - Mailing list pgsql-general

From Jon Sime
Subject Re: Age function
Date
Msg-id 4648C219.8000805@mediamatters.org
Whole thread Raw
In response to Age function  ("Andrus" <kobruleht2@hot.ee>)
List 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 by date:

Previous
From: "Alexander Staubo"
Date:
Subject: Re: Age function
Next
From: Rich Shepard
Date:
Subject: Re: How to implement GOMONTH function