dbTalk Databases Forums  

Total number of months from an interval

comp.databases.postgresql comp.databases.postgresql


Discuss Total number of months from an interval in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ben Finney
 
Posts: n/a

Default Total number of months from an interval - 06-29-2007 , 02:52 AM






Howdy all,

The PostgreSQL documentation says of the INTERVAL type:

Internally interval values are stored as months, days, and
seconds.
<URL:http://www.postgresql.org/docs/8.1/static/datatype-datetime.html#AEN4775>

I'm looking for a way to get the *total* number of months contained in
an interval value. E.g., I want something that provides the following
results:

interval '3 months' => 3
interval '1 year' => 12
interval '1 year 5 months' => 17
interval '2 years 4 months' => 28

Unfortunately, although PostgreSQL internally knows the total number
of months in an interval value, I can't find in the documentation a
way to get at that value. I suppose I could perform an explicit
calculation of years-times-12, but this seems needless overhead for a
query when the quantity I need is already stored directly in the
value.

Please show me how, within SQL, I can get at the
total-number-of-months quantity that the documentation says is stored
for an interval value.

--
\ "I took a course in speed waiting. Now I can wait an hour in |
`\ only ten minutes." -- Steven Wright |
_o__) |
Ben Finney

Reply With Quote
  #2  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: Total number of months from an interval - 06-29-2007 , 03:03 AM






begin Ben Finney schrieb:
Quote:
interval '2 years 4 months' => 28


Please show me how, within SQL, I can get at the
total-number-of-months quantity that the documentation says is stored
for an interval value.
test=*# select extract (years from interval '2 years 4 months') * 12 + extract(month from interval '2 years 4 months');
?column?
----------
28
(1 row)




end
Andreas
--
Andreas Kretschmer
Linux - weil ich es mir wert bin!
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net


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.