pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Calculating the age of a person
4+ messages / 4 participants
[nested] [flat]

* Calculating the age of a person
@ 2001-05-18 13:39  Hans-Jürgen Schönig <hs@cybertec.at>
  0 siblings, 1 reply; 4+ messages in thread

From: Hans-Jürgen Schönig @ 2001-05-18 13:39 UTC (permalink / raw)
  To: pgsql-sql

I have a table containing the birthdays of various persons. The target
is to compute the age of a persons.

persons=# SELECT age(birth), * FROM persons LIMIT 1;
              age              | id |  name  |   birth    | gender |
income
-------------------------------+----+--------+------------+--------+--------

 31 years 4 mons 16 days 23:00 |  1 | Albert | 1970-01-01 | m      |
35000
(1 row)

When I use age() I don't get full years. Is there an easy way to round
::reltime off or up without writing a function. Is there any possibility
to use plain SQL only?

    Hans




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Calculating the age of a person
@ 2001-05-18 15:53  tjk@tksoft.com <tjk@tksoft.com>
  0 siblings, 1 reply; 4+ messages in thread

From: tjk@tksoft.com @ 2001-05-18 15:53 UTC (permalink / raw)
  To: hs@cybertec.at; +Cc: pgsql-sql

You are probably looking for date_part().

E.g. 

SELECT date_part('year',age(birth)),* FROM persons LIMIT 1;


Troy



> 
> I have a table containing the birthdays of various persons. The target
> is to compute the age of a persons.
> 
> persons=# SELECT age(birth), * FROM persons LIMIT 1;
>               age              | id |  name  |   birth    | gender |
> income
> -------------------------------+----+--------+------------+--------+--------
> 
>  31 years 4 mons 16 days 23:00 |  1 | Albert | 1970-01-01 | m      |
> 35000
> (1 row)
> 
> When I use age() I don't get full years. Is there an easy way to round
> ::reltime off or up without writing a function. Is there any possibility
> to use plain SQL only?
> 
>     Hans
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> 




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Calculating the age of a person
@ 2001-05-20 10:07  Cedar Cox <cedarc@visionforisrael.com>
  parent: tjk@tksoft.com <tjk@tksoft.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Cedar Cox @ 2001-05-20 10:07 UTC (permalink / raw)
  To: Hans-Jürgen Schönig <hs@cybertec.at>; +Cc: pgsql-sql


Or you could use date_trunc() in the same way as date_part() if you want
an interval instead..

SELECT date_trunc('year',age(birth)),* FROM persons LIMIT 1;

On Fri, 18 May 2001, tjk@tksoft.com wrote:

> You are probably looking for date_part().
> 
> E.g. 
> 
> SELECT date_part('year',age(birth)),* FROM persons LIMIT 1;
> 
> 
> Troy
> 
> 
> 
> > 
> > I have a table containing the birthdays of various persons. The target
> > is to compute the age of a persons.
> > 
> > persons=# SELECT age(birth), * FROM persons LIMIT 1;
> >               age              | id |  name  |   birth    | gender |
> > income
> > -------------------------------+----+--------+------------+--------+--------
> > 
> >  31 years 4 mons 16 days 23:00 |  1 | Albert | 1970-01-01 | m      |
> > 35000
> > (1 row)
> > 
> > When I use age() I don't get full years. Is there an easy way to round
> > ::reltime off or up without writing a function. Is there any possibility
> > to use plain SQL only?
> > 
> >     Hans
> > 
> > 
> > ---------------------------(end of broadcast)---------------------------
> > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> > 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
> 




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Calculating the age of a person
@ 2001-05-22 14:20  Najm Hashmi <najm@mondo-live.com>
  parent: Hans-Jürgen Schönig <hs@cybertec.at>
  0 siblings, 0 replies; 4+ messages in thread

From: Najm Hashmi @ 2001-05-22 14:20 UTC (permalink / raw)
  To: Hans-Jürgen Schönig <hs@cybertec.at>; +Cc: pgsql-sql

Hans-Jürgen Schönig wrote:

> I have a table containing the birthdays of various persons. The target
> is to compute the age of a persons.
> 
> persons=# SELECT age(birth), * FROM persons LIMIT 1;
>               age              | id |  name  |   birth    | gender |
> income
> -------------------------------+----+--------+------------+--------+--------
> 
>  31 years 4 mons 16 days 23:00 |  1 | Albert | 1970-01-01 | m      |
> 35000
> (1 row)
> 
> When I use age() I don't get full years. Is there an easy way to round
> ::reltime off or up without writing a function. Is there any possibility
> to use plain SQL only?
> 
>     Hans
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Hey Hans try usting date_part function.... select  
date_part('year',birth)....
Regards

-- 
Najm Hashmi
Tel:514-271-9791
www.mondo-live.com
www.flipr.com




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2001-05-22 14:20 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-05-18 13:39 Calculating the age of a person Hans-Jürgen Schönig <hs@cybertec.at>
2001-05-22 14:20 ` Najm Hashmi <najm@mondo-live.com>
2001-05-18 15:53 Re: Calculating the age of a person tjk@tksoft.com <tjk@tksoft.com>
2001-05-20 10:07 ` Cedar Cox <cedarc@visionforisrael.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox