Re: Age function

From: "Alexander Staubo" <alex(at)purefiction(dot)net>
To: Andrus <kobruleht2(at)hot(dot)ee>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Age function
Date: 2007-05-14 20:04:49
Message-ID: 88daf38c0705141304t7c37e8f0xdfce3ec5280cb2f1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 5/14/07, Andrus <kobruleht2(at)hot(dot)ee> wrote:
> How to create function which returns persons age in years?
[snip]

What's wrong with age()?

# select age('1879-03-14'::date);
age
------------------
128 years 2 mons
# select extract(year from age('1879-03-14'::date));
date_part
-----------
128

You can give age() two arguments to calculate the difference between
two dates to get an interval:

# select age('1955-04-18'::date, '1879-03-14'::date);
age
-----------------------
76 years 1 mon 4 days

If you subtract two date values you will get the number of days
instead of an interval.

Documentation:

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

Alexander.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jon Sime 2007-05-14 20:10:01 Re: Age function
Previous Message Harpreet Dhaliwal 2007-05-14 17:23:08 Re: Database transaction related