简体   繁体   English

postgresql - 将间隔格式化为具有大量小时数的时间

[英]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.谢谢你的帮助。

Another solution:另一种解决方案:

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

You can build a custom representation with extract() :您可以使用extract()构建自定义表示:

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

Demo on DB Fiddle : 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 |

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.如果你想更多地参与进来,考虑几个月、几年等等,那么你可能需要构建一个 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM