Interval to months - Mailing list pgsql-general

From Aram Fingal
Subject Interval to months
Date
Msg-id B3AEA435-1203-43D7-85C8-4716AB506951@multifactorial.com
Whole thread Raw
Responses Re: Interval to months  (Steve Atkins <steve@blighty.com>)
List pgsql-general
I have a field which contains an interval value and I sometimes need to represent the full interval (not a part) as a
decimalnumber of months.  For example, "5 years 6 mons 3 days" as "66.1 months".  I've been trying to figure out how to
dothis and haven't found a definitive answer.   

The following gives an approximation:
round(cast(extract(epoch from time_interval)/2592000 as numeric), 2) || ' months'

The number 2592000 is seconds in a 30 day month.  Accounting for leap years, etc. Google calculates it as 2629743.83.
Thething is that the 30 day month number gives the right answer for short intervals while the Google number gives the
rightanswer for longer intervals (several years or more.) Is there a better way? 

--Aram

pgsql-general by date:

Previous
From: Adriaan Joubert
Date:
Subject: Re: Memory error in user-defined aggregation function
Next
From: Steve Atkins
Date:
Subject: Re: Interval to months