Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

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




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





Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group