将<integer>秒添加到PostgreSQL中的<timestamp> </timestamp> </integer>

时间:2015-02-15 16:05:25

标签: time casting timedelta

我有一个包含以下列的项目表:

  • start_time列(timestamp without time zone
  • expiration_time_seconds列(integer

例如,某些值为:

SELECT start_time, expiration_time_seconds 
   FROM whatever 
       ORDER BY start_time;

         start_time         | expiration_time_seconds
----------------------------+-------------------------
 2014-08-05 08:23:32.428452 |                  172800
 2014-08-10 09:49:51.082456 |                    3600
 2014-08-13 13:03:56.980073 |                    3600
 2014-08-21 06:31:38.596451 |                    3600
 ...

如何将以秒为单位的到期时间添加到start_time?

我尝试格式化interval命令的时间间隔字符串,但失败了:

blah=> SELECT interval concat(to_char(3600, '9999'), ' seconds');
ERROR:  syntax error at or near "("
LINE 1: SELECT interval concat(to_char(3600, '9999'), ' seconds');

1 个答案:

答案 0 :(得分:43)

trick是创建固定间隔并将其乘以列中的秒数:

SELECT start_time, 
       expiration_time_seconds, 
       start_time+expiration_time_seconds * interval '1 second'
   FROM whatever 
       ORDER BY start_time;

         start_time         | expiration_time_seconds |          end_time
----------------------------+-------------------------+----------------------------
 2014-08-05 08:23:32.428452 |                  172800 | 2014-08-07 08:23:32.428452
 2014-08-10 09:49:51.082456 |                    3600 | 2014-08-10 10:49:51.082456
 2014-08-13 13:03:56.980073 |                    3600 | 2014-08-13 14:03:56.980073
 2014-08-21 06:31:38.596451 |                    3600 | 2014-08-21 07:31:38.596451