Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

352
Views
postgresql - Format an interval as time with large number of hours

I have an interval that is calculated between events that can be days or hours between them.

Some rows show the desirable

00:15:38.687282

While other give me a worse-to-read

2 days 22:20:33.505085

How can I make the second example return the following?

70:22:33.505085

Thanks for any help.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Another solution:

select extract(epoch from interval '2 days 22:20:33.505085')/3600 * interval '1h';
┌─────────────────┐
│    ?column?     │
╞═════════════════╡
│ 70:20:33.505085 │
└─────────────────┘
(1 row)
over 4 years ago · Santiago Trujillo Report

0

You can build a custom representation with extract():

floor(extract(epoch from val) / 60 / 60)
|| ':' || extract(minute from val)
|| ':' || extract(second from val)

Demo on DB Fiddle:

select 
    floor(extract(epoch from val) / 60 / 60)
    || ':' || extract(minute from val)
    || ':' || extract(second from val) display
from (values ('2 days 22:20:33.505085'::interval)) as t(val)
| display         |
| :-------------- |
| 70:20:33.505085 |
over 4 years ago · Santiago Trujillo Report

0

A quick and dirty solution:

SELECT
   extract(days from '2 days 22:20:33.505085'::interval) * interval '24 hour' + '22:20:33.505085'::interval

 ?column?     
-----------------
 70:20:33.505085

If you want to get more involved then that, account for months, years and so on then you will probably need to build a function.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!