Re: formatting intervals with to_char

From: "Aaron Bono" <postgresql(at)aranya(dot)com>
To: gdavis(at)refractions(dot)net
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: formatting intervals with to_char
Date: 2006-10-05 04:39:40
Message-ID: bf05e51c0610042139t54587d33q374932d62da47ce0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

On 10/4/06, Graham Davis <gdavis(at)refractions(dot)net> wrote:
>
> I haven't heard any replies from this, so in the meantime I've found a
> hacky way to get the output I desire. I'm basically calculating the
> hours on the fly and piecing together a formatted string with
> concatenations like this:
>
> SELECT
> (((EXTRACT(day from time_idle)*24)+EXTRACT(hour from time_idle)) || ':' ||
> EXTRACT(minute from time_idle) || ':' || EXTRACT(second from
> time_idle))::interval AS myinterval
> FROM
> ( SELECT ('2006-09-15T23:59:00'::timestamp - '2006-09-01
> 09:30:41'::timestamp) AS time_idle) FROM_TABLE;
>
> If anyone knows a better/proper way to get this result, let me know.

That would be my approach though you could also replace:

EXTRACT(minute from time_idle) || ':' || EXTRACT(second from time_idle)

with:

to_char(time_idle, ':MM:SS')

If you really want to clean up your SQL you could create a function that
takes any timestamp and then make the function IMMUTABLE to tell postgresql
it doesn't need to rederive the results every time.

==================================================================
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
http://codeelixir.com
==================================================================

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2006-10-05 08:35:01 Re: Netflix Prize data
Previous Message David Fetter 2006-10-05 04:39:31 Re: Digging gram.y

Browse pgsql-sql by date

  From Date Subject
Next Message Achilleas Mantzios 2006-10-05 07:32:55 Re: i have table
Previous Message James Baxendale 2006-10-05 04:33:50 psql hangs during load of pg_dumpall file