Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id A5F7C2E2DE1 for ; Sun, 27 Jan 2008 23:10:48 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 31109-08 for ; Sun, 27 Jan 2008 23:10:44 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 06D962E2D85 for ; Sun, 27 Jan 2008 23:10:43 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id m0S3Aebm016844; Sun, 27 Jan 2008 22:10:40 -0500 (EST) To: Bryce Nesbitt cc: pgsql-sql@postgresql.org Subject: Re: extract or date_part on an interval? How many e In-reply-to: <479D3E63.2080601@obviously.com> References: <479D3E63.2080601@obviously.com> Comments: In-reply-to Bryce Nesbitt message dated "Sun, 27 Jan 2008 18:30:59 -0800" Date: Sun, 27 Jan 2008 22:10:40 -0500 Message-ID: <16843.1201489840@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/211 X-Sequence-Number: 30064 Bryce Nesbitt writes: > Hmm. Seemed so simple. But how do I get the number of years an > interval represents? extract is clearly the wrong way: There is nothing simple about datetime calculations, ever :-( Let me exhibit why this particular case is not as simple as you could wish: regression=# select '2007-02-01'::timestamp + interval '1 year'; ?column? --------------------- 2008-02-01 00:00:00 (1 row) regression=# select '2007-02-01'::timestamp + interval '365 days'; ?column? --------------------- 2008-02-01 00:00:00 (1 row) regression=# select '2008-02-01'::timestamp + interval '1 year'; ?column? --------------------- 2009-02-01 00:00:00 (1 row) regression=# select '2008-02-01'::timestamp + interval '365 days'; ?column? --------------------- 2009-01-31 00:00:00 (1 row) That is, there isn't any fixed conversion factor between N days and N years, so the interval datatype treats them as incommensurate. If you're willing to settle for an approximate answer, you can do extract(epoch from interval) and then divide by however many seconds you want to believe are in a year. This will give various wrong answers in various corner cases, but I'm not sure there is a right answer. regards, tom lane