Re: Converting time to float - Mailing list pgsql-general

From Richard Huxton
Subject Re: Converting time to float
Date
Msg-id 4631E588.2060902@archonet.com
Whole thread Raw
In response to Converting time to float  (Jorge Godoy <jgodoy@gmail.com>)
Responses Re: Converting time to float  (Jorge Godoy <jgodoy@gmail.com>)
List 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

pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: Vacuum-full very slow
Next
From: Jorge Godoy
Date:
Subject: Re: Converting time to float