Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 465072E3234 for ; Mon, 28 Jan 2008 01:37:46 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 31040-01-3 for ; Mon, 28 Jan 2008 01:37:40 -0400 (AST) Received: from proton.planetargon.com (proton.planetargon.com [198.145.115.11]) by postgresql.org (Postfix) with ESMTP id 6CDA42E3207 for ; Mon, 28 Jan 2008 01:20:14 -0400 (AST) Received: by proton.planetargon.com (Postfix, from userid 502) id 299BC8336DE; Sun, 27 Jan 2008 21:19:48 -0800 (PST) Received: from [10.0.1.200] (c-67-165-16-168.hsd1.pa.comcast.net [67.165.16.168]) by proton.planetargon.com (Postfix) with ESMTP id 0A27986CF20; Sun, 27 Jan 2008 21:19:16 -0800 (PST) In-Reply-To: <479D5F54.3040107@obviously.com> References: <479D3E63.2080601@obviously.com> <16843.1201489840@sss.pgh.pa.us> <479D5F54.3040107@obviously.com> Mime-Version: 1.0 (Apple Message framework v753) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: extract or date_part on an interval? How many e Date: Mon, 28 Jan 2008 00:19:04 -0500 To: Bryce Nesbitt X-Mailer: Apple Mail (2.753) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/213 X-Sequence-Number: 30066 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