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

标签 time casting timedelta

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

  • start_time 列(不带时区的时间戳)
  • expiration_time_seconds 列(整数)

例如,一些值是:

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');

最佳答案

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

关于time - 将 <integer> 秒添加到 PostgreSQL 中的 <timestamp>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28528028/

相关文章:

haskell - 如何定义 listTree 使其在线性时间内运行?

php - 精确秒到 "day/month/year - minutes : hours : seconds"转换

java - 错误: Type mismatch: cannot convert from List<Integer> to ArrayList<Integer>

python - 从 Pandas 的timedelta列中提取天数

python - 排除周末的两天之间的差异(以小时为单位)

javascript - 如何从当前时间 momentjs 中减去时间

java - 如何对分钟值进行舍入?

java - 是将编程转换为实现吗?

vb.net - 哪个更有效 Cstr(value) 或 value.ToString()

python - 在 python 中以分钟为单位获取两个日期时间对象之间的差异