Re: Converting time to float

From: Richard Huxton <dev(at)archonet(dot)com>
To: Jorge Godoy <jgodoy(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Converting time to float
Date: 2007-04-27 11:59:04
Message-ID: 4631E588.2060902@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jorge Godoy wrote:
> Hi!
>
>
> I am needing to convert from TIME type to floats and taking 1h as the
> unit, so what I'd like is something that would allow me doing operations
> like:
>
> SELECT '00:10:00'::TIME / '1:00:00'::TIME; -- Answer is: 0.16666666666666666667

But it doesn't make any sense to divide one time by another, does it?
Are you sure it's not intervals you want?

Anyway, try something like this:

SELECT extract(epoch from ('14:02:04'::time)) / extract(epoch from
('01:00:00'::time));
?column?
------------------
14.0344444444444

The "epoch" is in seconds (from midnight 1970-01-01 for timestamps) so
gives you the result you want.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message rupesh bajaj 2007-04-27 12:01:41 When the locially dropped column is also physically dropped
Previous Message Martijn van Oosterhout 2007-04-27 11:47:46 Re: Vacuum-full very slow