Solved

Time conversion to Metric

Posted on 2011-03-15
7
428 Views
Last Modified: 2012-05-11
Hello Experts!

I am looking for this Postgres function that will take a "timestamp without time zone" value and convert it to metric time.

for example:
time                             metric time [hh.mm]
19 days 06:17:00      462.28
155:05:00                     155.08
0
Comment
Question by:daniel_spiri
7 Comments
 
LVL 10

Expert Comment

by:borgunit
ID: 35137607
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35140187
You could extract the epoch (which does second) and convert
EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours')/3600;
This would give hours since 60*60 = 3600
0
 
LVL 22

Expert Comment

by:earth man2
ID: 35152895
do you mean interval instead of timestamp ?
select round(cast ( extract(epoch from interval '19 days 06:17:00')as numeric )/3600.0, 2);
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:daniel_spiri
ID: 35274744
Earthman2,
When I run the query:

select round(cast ( extract(epoch from interval '19 days 06:17:00')as numeric )/3600.0, 2);

Result: 462.28

The minutes don't seem right.

The query on '19 days 06:01:00' returns 462.02.   (but it should be 462.01)
0
 
LVL 22

Accepted Solution

by:
earth man2 earned 500 total points
ID: 35286039
select div( cast (extract ( epoch from interval '19 days 06:17:00')as integer),3600)::text || ':' || extract( minute from interval '19 days 06:17:00');
0
 

Author Comment

by:daniel_spiri
ID: 35333541
Also, can you please help me in transforming this into a function?
0
 
LVL 22

Expert Comment

by:earth man2
ID: 35354874
create or replace function interval2dec( interval )returns text as $$
select div(cast (extract( epoch from $1)as integer),3600)::text || ':' || extract( minute from $1);
$$ language SQL;

select interval2dec( '19 days 06:17:00'::interval);
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question