PostgreSQL返回的奇怪格式的区间值

时间:2016-02-04 10:14:52

标签: sql postgresql time intervals

我有PostgreSQL 9.5.0,我观察到非常相似的查询返回的奇怪间隔值。我认为然后间隔大于24小时然后返回days这样的部分:

SELECT (1000 * ('2016-02-04 12:00:11'::timestamp - '2016-02-03 12:00:00'::timestamp)::interval)::interval

1000 days 03:03:20

但有时候小时数不会转换为天数:

SELECT (1000 * ('2016-02-04 11:00:11'::timestamp - '2016-02-03 12:00:00'::timestamp)::interval)::interval

23003:03:20

如何使用days部分强制使用相同的输出格式?

1 个答案:

答案 0 :(得分:4)

使用justify_hours()

SELECT justify_hours(1000 * ('2016-02-04 11:00:11'::timestamp - '2016-02-03 12:00:00'))

   justify_hours   
-------------------
 958 days 11:03:20
(1 row) 

justify_interval()

SELECT justify_interval(1000 * ('2016-02-04 11:00:11'::timestamp - '2016-02-03 12:00:00'))

        justify_interval         
---------------------------------
 2 years 7 mons 28 days 11:03:20
(1 row) 

另请参阅:Why postgres show two different format for same interval value?