如何在PostgreSQL视图中获取两个日期时间字段的天数和小时数差异? [英] How to get two datetime fields difference in days and hours in postgresql view?

查看:657
本文介绍了如何在PostgreSQL视图中获取两个日期时间字段的天数和小时数差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下表所示:

  **start_date**                         **end_date**
---------------                      -----------------
2011-06-27 13:24:45.137417          2011-06-28 05:34:26.54939
2011-09-30 09:09:00.501381          2011-10-03 23:38:46.479824

预期结果如下:

Diff
-------
0.16
3.14

其中小数点前的数字是天数和小数点后是小时

where the digits before the decimal point is "Days" & after decimal point is "Hours"

我尝试使用
extract((end_date-start_date)中的'epoch')/(3600 * 24)

i try it using extract('epoch' from (end_date-start_date))/(3600*24)

,但它以100的格式给出小时值,即第一条记录&的0.673395971909722 3.60400438012731(第二条记录)

but it gives the "hours" value in format of 100 i.e 0.673395971909722 for the first record & 3.60400438012731 for second record

推荐答案

请参见文档

SELECT to_char(end_date - start_date, 'D.HH24') AS diff FROM your_table;




0.16
3.14

希望有帮助。

这篇关于如何在PostgreSQL视图中获取两个日期时间字段的天数和小时数差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆