Home > Archive > PostgreSQL Discussion > April 2005 > Intervals









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Intervals
Jake Stride

2005-04-26, 7:23 am

Is there a way to convert in interval into hours? I have a table that
records the amount of time worked by a person and want to sum up all the
hours, however with the column being an interval once you reach more
than 24 hours it turns that into a day. This is not what I want so
instead of outputting 1day 2:00:00 I would want to output 26:00:00 is
this possible?

Thanks

Jake

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Michael Glaesemann

2005-04-26, 7:23 am


On Apr 26, 2005, at 18:47, Jake Stride wrote:

> Is there a way to convert in interval into hours? I have a table that
> records the amount of time worked by a person and want to sum up all
> the
> hours, however with the column being an interval once you reach more
> than 24 hours it turns that into a day. This is not what I want so
> instead of outputting 1day 2:00:00 I would want to output 26:00:00 is
> this possible?


Search the archives from the past couple of days of the pgsql-sql
mailing list with the subject line Re: [SQL] to_char(interval) ? and
you should find a solution. The archives can be found at
http://archives.postgresql.org

Hope this helps.

Michael Glaesemann
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere
" to majordomo@postgresql
.org)

Dawid Kuroczko

2005-04-26, 7:23 am

On 4/26/05, Jake Stride <nsuk@users.sourceforge.net> wrote:
> Is there a way to convert in interval into hours? I have a table that
> records the amount of time worked by a person and want to sum up all the
> hours, however with the column being an interval once you reach more
> than 24 hours it turns that into a day. This is not what I want so
> instead of outputting 1day 2:00:00 I would want to output 26:00:00 is
> this possible?


Something along lines of select extract(epoch from '1 day 2
hours'::interval) / 3600
maybe?

Regards,
Dawid

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Andrei Gaspar

2005-04-26, 7:23 am

I had the same problem and wrote a small function

create function hours(timestamp without time zone, timestamp without
time zone) RETURNS integer as
$$select cast( (cast($2 as date) - cast($1 as date)) * 24 + extract(hour
from cast($2 as time) - cast($1 as time)) as integer)$$ language SQL
IMMUTABLE;

Andrei

Jake Stride wrote:

>Is there a way to convert in interval into hours? I have a table that
>records the amount of time worked by a person and want to sum up all the
>hours, however with the column being an interval once you reach more
>than 24 hours it turns that into a day. This is not what I want so
>instead of outputting 1day 2:00:00 I would want to output 26:00:00 is
>this possible?
>
>Thanks
>
>Jake
>
>---------------------------(end of broadcast)---------------------------
>TIP 8: explain analyze is your friend
>
>
>
>



--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.10.3 - Release Date: 4/25/2005


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Jake Stride

2005-04-26, 1:23 pm

That is helpful, I have created a slightly different function that
returns an interval in the format HH:MM not sure if it will help anyone
or anyone has any suggestions to improve it:

create function hours(interval) returns varchar as 'SELECT
floor(extract(epoch from $1)/3600) || ':' || (cast(extract(epoch FROM
$1) AS integer)%3600)/60;' language SQL IMMUTABLE;

Thanks

Jake

Andrei Gaspar wrote:

> I had the same problem and wrote a small function
>
> create function hours(timestamp without time zone, timestamp without
> time zone) RETURNS integer as
> $$select cast( (cast($2 as date) - cast($1 as date)) * 24 +
> extract(hour from cast($2 as time) - cast($1 as time)) as integer)$$
> language SQL IMMUTABLE;
>
> Andrei
>
> Jake Stride wrote:
>
>
>



--
Jake Stride

Senokian Solutions Ltd
The TechnoCentre
Coventry University Technology Park
Puma Way
Coventry
CV1 2TT

T: 0870 744 2030
F: 0870 460 2623
M: 07713 627 304
E: jake.stride@senokian.com


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql
.org

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com