如何在postgresql中将时间间隔转换为十进制值?

时间:2017-04-02 17:54:27

标签: sql date casting amazon-redshift

运行此查询时:

SELECT
  start_date,
  end_date,
  end_date-start_date as timediff

FROM content_events

我得到这样一张桌子:

+---------------------+---------------------+-----------------+
| start_date          | end_date            | timediff        |
+---------------------+---------------------+-----------------+
| 2017-03-23 23:00:00 | 2017-04-01 22:00:00 | 8 days 23:00:00 |
+---------------------+---------------------+-----------------+

但我想得到这样的东西:

+---------------------+---------------------+-----------------+
| start_date          | end_date            | timediff        |
+---------------------+---------------------+-----------------+
| 2017-03-23 23:00:00 | 2017-04-01 22:00:00 | 8,96            |
+---------------------+---------------------+-----------------+

请注意,我只需要将时差作为带有两位小数的数值。

我正在使用PostgreSQL 8.0.2。

非常感谢!

1 个答案:

答案 0 :(得分:1)

这个select语句可以正常工作:

(date_part(epoch,end_date)::float - date_part(epoch,start_date)::float)/(24*60*60) as timediff

或者这个:

(extract(epoch from end_date)::float - extract(epoch from start_date)::float)/(24*60*60) as timediff