【问题标题】:Compute Difference Between Two Timestamps计算两个时间戳之间的差异
【发布时间】:2020-05-13 03:47:14
【问题描述】:

我在 PostgreSQL 表中有两个 Timestamp 列(例如,2019-07-12 07:59:59)。我需要减去两列并将 (HH:MM:SS) 值保存为新名称?

【问题讨论】:

标签: sql postgresql datediff sql-timestamp


【解决方案1】:

无需计算历元。减去时间戳给出了一个直接转换的区间:

with timestamps (tsa, tsb) as 
     (values ('2020-01-15T06:15:00'::timestamp, '2020-01-15T18:15:00'::timestamp)) 
select to_char(tsb-tsa,'hh24:mi:ss') from timestamps; 

【讨论】:

    【解决方案2】:

    您将两者结合起来: 先得到以秒为单位的差,以 hhmmss 为单位转换秒数:

    SELECT TO_CHAR((EXTRACT(EPOCH 
                   FROM (timestamp_B - timestamp_A)) || ' second')::interval, 'HH24:MI:SS')
    

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多