473,771 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('Yea r', CURRENT_DATE) -date_part('Year ', SOMEDATE)) * 12
+date_part('Mon th', CURRENT_DATE)-date_part('Mont h',SOMEDATE)

TIA
Patrick Hatcher

Nov 23 '05 #1
2 3791
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(YE AR 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('Yea r', CURRENT_DATE) -date_part('Year ', SOMEDATE)) * 12
+date_part('Mon th', CURRENT_DATE)-date_part('Mont h',SOMEDATE)

TIA

Patrick Hatcher



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

iD8DBQBBiTrT21d VnhLsBV0RAv9aAK CNxn+LVukz2VntV lzCoXmo9/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(YE AR 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('Yea r', CURRENT_DATE) -date_part('Year ', SOMEDATE)) * 12
+date_part('Mon th', CURRENT_DATE)-date_part('Mont h',SOMEDATE)

TIA

Patrick Hatcher



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

iD8DBQBBiTrT21d VnhLsBV0RAv9aAK CNxn+LVukz2VntV lzCoXmo9/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
9717
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 Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
3
3354
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. Here is the formula I am using now to get the number of months: #ofMonths: Month(.-.)-1. It has worked for me until now because it never was greater than a year before. Please help. I am a novice at this.
6
27994
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 to the right of these 2 fields to show the
7
16243
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
0
1778
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, 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
37
2297
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
7336
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 that there's something called "month_abbr" in the calendar module. However, when I try to do calendar.month_abbr.index("Jan"), I get "_localized_month instance has no attribute 'index'." So it seems that month_abbr isn't a regular list. I'm...
4
2480
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. table A contains customer information. table B contains all the bills for each customer. Table B has fields: customerID, Rate, and the Date. I have a field in the customer table (table A) which contains how many months that they still owe for. I need a...
16
3669
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 I only show maybe 3 months of items at a time. The items have to be ordered according to the year, IOW I can't list something in April as being #1. In HTML after the first couple of months I just use this sort of code: <ol start="14"...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.