By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Number of months

P: n/a
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

Nov 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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:
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-----

Nov 23 '05 #2

P: n/a
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:
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-----

Nov 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.