Re: extract or date_part on an interval? How many e

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Bryce Nesbitt <bryce1(at)obviously(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: extract or date_part on an interval? How many e
Date: 2008-01-28 05:19:04
Message-ID: EA81A141-5E8B-4C82-8B9E-209817E62EC9@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Jan 27, 2008, at 23:51 , Bryce Nesbitt wrote:

> Sigh. Ok, I settled on:
> select '1987-01-29'::timestamp + interval '21 years' > now();
> Which is closer to what I wanted anyway (this was all about
> determining who was under 21 years old). This at least should be
> robust over leap years.

I think this should work for you for your particular case:

EXTRACT(year FROM AGE(born_on)) >= 21

SELECT born_on, current_date, age(born_on), EXTRACT(year FROM AGE
(born_on)) AS age_in_years, EXTRACT(year FROM age(born_on)) >= 21 as
old_enough_to_drink FROM (SELECT CAST('1987-01-25' AS DATE) + i AS
born_on FROM generate_series(0,5) as the(i)) foo;
born_on | date | age | age_in_years |
old_enough_to_drink
------------+------------+--------------------------+--------------
+---------------------
1987-01-25 | 2008-01-28 | 21 years 3 days | 21 | t
1987-01-26 | 2008-01-28 | 21 years 2 days | 21 | t
1987-01-27 | 2008-01-28 | 21 years 1 day | 21 | t
1987-01-28 | 2008-01-28 | 21 years | 21 | t
1987-01-29 | 2008-01-28 | 20 years 11 mons 30 days | 20 | f
1987-01-30 | 2008-01-28 | 20 years 11 mons 29 days | 20 | f
(6 rows)

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Premsun Choltanwanich 2008-01-28 07:18:24 Slow Query problem
Previous Message Bryce Nesbitt 2008-01-28 04:51:32 Re: extract or date_part on an interval? How many e