More straight forward method to convert seconds::bigint to interval

Started by Shane Spencerover 16 years ago4 messagesgeneral
Jump to latest
#1Shane Spencer
shane@bogomip.com

I work in VoIP. HMS (Hour/Minute/Second) format appears to be the rule
when working with call time totals. I admit it makes some reports
easier to read.

The method I used to convert a int/bigint to HMS (or the standard
representation of an interval type) is as follows:

select (123456.789::varchar(24) || ' seconds')::interval as HMS;
hms
--------------
34:17:36.789

Is there a less string oriented method of converting seconds as an int
to an interval?

- Shane

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Shane Spencer (#1)
Re: More straight forward method to convert seconds::bigint to interval

"Shane R. Spencer" <shane@bogomip.com> writes:

Is there a less string oriented method of converting seconds as an int
to an interval?

Multiply by an interval, eg

123456.789 * interval '1 second'

It works, it's fast, and you can use any scale factor you want.

regards, tom lane

#3Adam Rich
adam.r@sbcglobal.net
In reply to: Shane Spencer (#1)
Re: More straight forward method to convert seconds::bigint to interval

Shane R. Spencer wrote:

I work in VoIP. HMS (Hour/Minute/Second) format appears to be the rule
when working with call time totals. I admit it makes some reports
easier to read.

The method I used to convert a int/bigint to HMS (or the standard
representation of an interval type) is as follows:

select (123456.789::varchar(24) || ' seconds')::interval as HMS;
hms
--------------
34:17:36.789

Is there a less string oriented method of converting seconds as an int
to an interval?

- Shane

I think this is cleaner/faster:

select interval '1 second' * 123456.789 as HMS;

hms
--------------
34:17:36.789

-Adam

#4Shane Spencer
shane@bogomip.com
In reply to: Adam Rich (#3)
Re: More straight forward method to convert seconds::bigint to interval

Well worth joining the list. Thanks guys.

Show quoted text

On Wed, Sep 23, 2009 at 12:48 PM, Adam Rich <adam.r@sbcglobal.net> wrote:

Shane R. Spencer wrote:

I work in VoIP.  HMS (Hour/Minute/Second) format appears to be the rule
when working with call time totals.  I admit it makes some reports
easier to read.

The method I used to convert a int/bigint to HMS (or the standard
representation of an interval type) is as follows:

select (123456.789::varchar(24) || ' seconds')::interval as HMS;
    hms
--------------
 34:17:36.789

Is there a less string oriented method of converting seconds as an int
to an interval?

- Shane

I think this is cleaner/faster:

select interval '1 second' * 123456.789 as HMS;

    hms
--------------
 34:17:36.789

-Adam