[英]How to convert the current_timestamp time to 24 hours format in postgresql
[英]postgresql - Format an interval as time with large number of hours
我有一個在事件之間計算的間隔,它們之間可能是幾天或幾小時。
一些行顯示了可取的
00:15:38.687282
而其他的讓我讀起來更糟
2 days 22:20:33.505085
如何使第二個示例返回以下內容?
70:22:33.505085
謝謝你的幫助。
另一種解決方案:
select extract(epoch from interval '2 days 22:20:33.505085')/3600 * interval '1h';
┌─────────────────┐
│ ?column? │
╞═════════════════╡
│ 70:20:33.505085 │
└─────────────────┘
(1 row)
您可以使用extract()
構建自定義表示:
floor(extract(epoch from val) / 60 / 60)
|| ':' || extract(minute from val)
|| ':' || extract(second from val)
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 |
一個快速而骯臟的解決方案:
SELECT
extract(days from '2 days 22:20:33.505085'::interval) * interval '24 hour' + '22:20:33.505085'::interval
?column?
-----------------
70:20:33.505085
如果你想更多地參與進來,考慮幾個月、幾年等等,那么你可能需要構建一個 function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.