473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Number of months

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
2 3776
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher Benson-Manica | last post by:
What would be the best way to convert a month number to its corresponding string? I.e, 3 -> 'March'. Is there a builtin function or must I use a lookup table or something? -- Christopher...
3
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. ...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
7
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
0
by: Patrick Hatcher | last post by:
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,...
37
by: Gregc. | last post by:
G'day I'm trying to work out the number of years since 1970, here is my code: include <stdio.h> #include <time.h> const int SEC_IN_MIN = 60; const int SEC_IN_HOUR = SEC_IN_MIN * 60;
10
by: Bill | last post by:
Hello -- I'm parsing the output of the finger command, and was wondering something...If I'm given a month abbrievation (such as "Jan"), what's the best way to figure out the month number? I see...
4
by: wishwish20 | last post by:
Hello, I am having trouble with an SQL query which is used as a source on a report. The database is being used to keep track of billing information. I have made an sql query, which has 2 tables....
16
by: some old html fumbler | last post by:
I want to use CSS to get an ordered list to start with something other than a 1 or equivalent. I have a list of things that happen during the course of a year. Too long a list is not helpful, so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.