【问题标题】:Difference between 2 dates in PostgreSQLPostgreSQL 中两个日期之间的差异
【发布时间】:2015-10-15 10:48:28
【问题描述】:

我在 Postgres 数据库中的表中有 2 个“时间戳”列。我需要提取这两列之间的天数差异。让我们考虑列是“column1”和“column2”。这两个时间戳之间的差异是 10 小时。 所以,对于查询:

select column2 - column1 from table

结果为“间隔”类型,为“10:00:00”。我需要将其转换为实数,并且在本例中,要获得类似“0.41666...7”的结果

【问题讨论】:

  • I need to convert this in a real number 我不知道你的意思是什么?例如 10:32:41 的值是多少?
  • "实数" - 相当于 Java 中的 "double" 值

标签: postgresql timestamp difference


【解决方案1】:

您可以使用 extract() 从结果间隔中获取秒数

select extract(epoch from column2 - column1)
from the_table;

如果你把它除以 86400 得到它作为一天的一小部分

【讨论】:

    【解决方案2】:
    DATE_PART('day', column2 - column1)
    

    http://www.sqlines.com/postgresql/how-to/datediff

    【讨论】:

    • 这将返回一个整数值。我需要一个带小数的值。使用 date_part,结果为“0”。
    • 查看我链接的参考资料。 DATE_PART('hour', end - start )/24 应该给你一天的额外小数部分。所以整个答案是 DATE_PART('day', column2 - column1) + DATE_PART('hour', end - start )/24
    • 这看起来可能纯粹是一分钟,而不考虑其他分母,例如对于DATE_PART('minute', time_1 - time_2) < 1,我得到 time_1 = 2019-06-21 09:32:45.073002 和 time_2 = 2019-06-24 04:32:47.779387 返回
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2011-10-29
    • 1970-01-01
    • 2020-10-19
    • 2013-10-02
    • 2012-01-15
    • 2016-12-30
    • 2011-06-13
    相关资源
    最近更新 更多