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/

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Reply via email to