间隔转换

您可以使用 CAST 将字符串转换为间隔,反之亦然。

字符串到间隔

您可以如下所示将字符串转换为间隔:

CAST( [ INTERVAL[(p)] ] [-] ] interval‑literal AS INTERVAL[(p)] interval‑qualifier )

例如:

=> SELECT CAST('3700 sec' AS INTERVAL);
 ?column?
----------
 01:01:40

您可以在日期-时间或年-月子类型中投射转换间隔,但不能在其之间转换:

=> SELECT CAST(INTERVAL '4440' MINUTE as INTERVAL);
  ?column?
----------
 3 days 2 hours
=> SELECT CAST(INTERVAL -'01:15' as INTERVAL MINUTE);
 ?column?
----------
 -75 mins

间隔到字符串

您可以如下所示将间隔转换为字符串:

CAST( (SELECT interval ) AS VARCHAR[(n)] )

例如:

=> SELECT CONCAT(
  'Tomorrow at this time: ',
  CAST((SELECT INTERVAL '24 hours') + CURRENT_TIMESTAMP(0) AS VARCHAR));
                    CONCAT
-----------------------------------------------
 Tomorrow at this time: 2016-08-17 08:41:23-04
(1 row)