dbTalk Databases Forums  

Number of months

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Number of months in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Patrick Hatcher
 
Posts: n/a

Default Number of months - 11-03-2004 , 12:52 PM






Is there a function that will give me the number of months, as an
integer, in Pg 7.4.x? I found the date_trunc function but that will
return text and I didn't see anything else?

I have this, but didn't want to duplicate the work if it wasn't necessary:

(date_part('Year', CURRENT_DATE) -date_part('Year', SOMEDATE)) * 12
+date_part('Month', CURRENT_DATE)-date_part('Month',SOMEDATE)

TIA


Patrick Hatcher




Reply With Quote
  #2  
Old   
Franco Bruno Borghesi
 
Posts: n/a

Default Re: Number of months - 11-03-2004 , 02:08 PM






You could create your own function for the conversion, something like:

CREATE OR REPLACE FUNCTION interval2Months(INTERVAL) RETURNS INTEGER
LANGUAGE 'sql' IMMUTABLE AS '
SELECT CAST(extract(YEAR FROM $1) * 12 + extract (MONTH FROM $1) AS
INTEGER);
';

you call it doing SELECT interval2Months(age('2003-01-01'::date));

Of course, you can create a function that obtains the age directly from
a DATE parameter and then converts that value to months.

Hope it helps.

On Wed, 2004-11-03 at 15:52, Patrick Hatcher wrote:

Quote:
Is there a function that will give me the number of months, as an
integer, in Pg 7.4.x? I found the date_trunc function but that will
return text and I didn't see anything else?

I have this, but didn't want to duplicate the work if it wasn't
necessary:

(date_part('Year', CURRENT_DATE) -date_part('Year', SOMEDATE)) * 12
+date_part('Month', CURRENT_DATE)-date_part('Month',SOMEDATE)

TIA

Patrick Hatcher


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (FreeBSD)

iD8DBQBBiTrT21dVnhLsBV0RAv9aAKCNxn+LVukz2VntVlzCoX mo9/iFagCdGKtR
L4bFqC1X/MSZd+HCUgz+ZXw=
=bK50
-----END PGP SIGNATURE-----



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.