更新时间:2024-11-27 GMT+08:00

时间/日期操作符

用户在使用时间和日期操作符时,对应的操作数请使用明确的类型前缀修饰,以确保数据库在解析操作数的时候能够与用户预期一致,不会产生用户非预期的结果。

比如下面示例没有明确数据类型就会出现异常错误。

1
2
SELECT date '2001-10-01' - '7' AS RESULT;
ERROR:  invalid input syntax for type timestamp: "7"
表1 时间和日期操作符

操作符

示例

+

date类型参数与integer参数相加,获取时间间隔为7天后的时间:

1
2
3
4
5
SELECT date '2001-09-28' + integer '7' AS RESULT;
       result        
---------------------
 2001-10-05 00:00:00
(1 row)

date类型参数与interval参数相加,获取时间间隔为1小时后的时间:

1
2
3
4
5
SELECT date '2001-09-28' + interval '1 hour' AS RESULT;
       result        
---------------------
 2001-09-28 01:00:00
(1 row)

date类型参数与time类型参数相加,获取具体的日期和时间结果:

1
2
3
4
5
SELECT date '2001-09-28' + time '03:00' AS RESULT;
       result        
---------------------
 2001-09-28 03:00:00
(1 row)

date类型参数与interval参数相加,获取时间间隔为1个月的时间:

date函数对于日期相加减超过月份的日期范围,会对齐到对应月份最后一天,不超过则不处理。例如:2021-01-31后一个月的日期为2021-02-31,但2月为闰月,只有28天,那么date函数会返回对齐到2月份最后一天的结果,即2021-02-28。

1
2
3
4
5
SELECT date '2021-01-31' + interval '1 month' AS RESULT;
       result
---------------------
 2021-02-28 00:00:00
(1 row)
1
2
3
4
5
SELECT date '2021-02-28' + interval '1 month' AS RESULT;
       result
---------------------
 2021-03-28 00:00:00
(1 row)

interval参数相加,获取两个时间间隔之和:

1
2
3
4
5
SELECT interval '1 day' + interval '1 hour' AS RESULT;
     result     
----------------
 1 day 01:00:00
(1 row)

timestamp时间类型参数与interval参数相加,获取间隔23小时后的时间:

1
2
3
4
5
SELECT timestamp '2001-09-28 01:00' + interval '23 hours' AS RESULT;
       result        
---------------------
 2001-09-29 00:00:00
(1 row)

time类型参数与interval参数相加,获取间隔时间为3小时后的时间:

1
2
3
4
5
SELECT time '01:00' + interval '3 hours' AS RESULT;
  result  
----------
 04:00:00
(1 row)

-

date类型参数相减,获取两个日期的时间差:

1
2
3
4
5
SELECT date '2001-10-01' - date '2001-09-28' AS RESULT;
 result 
--------
 3 days
(1 row)

date类型参数与integer参数相减,返回timestamp类型,获取两者的时间差:

1
2
3
4
5
SELECT date '2001-10-01' - integer '7' AS RESULT;
       result        
---------------------
 2001-09-24 00:00:00
(1 row)

date类型参数与interval参数相减,获取两者的日期、时间差:

1
2
3
4
5
SELECT date '2001-09-28' - interval '1 hour' AS RESULT;
       result        
---------------------
 2001-09-27 23:00:00
(1 row)

time类型参数相减,获取两参数的时间差:

1
2
3
4
5
SELECT time '05:00' - time '03:00' AS RESULT;
  result  
----------
 02:00:00
(1 row)

time类型参数与interval相减,获取两参数的时间差:

1
2
3
4
5
SELECT time '05:00' - interval '2 hours' AS RESULT;
  result  
----------
 03:00:00
(1 row)

timestamp类型参数与interval相减,从时间戳中减去时间间隔,获取两者的日期时间差:

1
2
3
4
5
SELECT timestamp '2001-09-28 23:00' - interval '23 hours' AS RESULT;
       result        
---------------------
 2001-09-28 00:00:00
(1 row)

interval参数相减,获取两者的时间差:

1
2
3
4
5
SELECT interval '1 day' - interval '1 hour' AS RESULT;
  result  
----------
 23:00:00
(1 row)

timestamp类型参数相减,获取两者的日期时间差:

1
2
3
4
5
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' AS RESULT;
     result     
----------------
 1 day 15:00:00
(1 row)

获取当前日期的前一天:

1
2
3
4
5
SELECT now() - interval '1 day'AS RESULT;
           result
-------------------------------
 2022-08-08 01:46:15.555406+00
(1 row)

*

将时间间隔乘以数量:

1
2
3
4
5
SELECT 900 * interval '1 second' AS RESULT;
  result  
----------
 00:15:00
(1 row)
1
2
3
4
5
SELECT 21 * interval '1 day' AS RESULT;
 result  
---------
 21 days
(1 row)
1
2
3
4
5
SELECT double precision '3.5' * interval '1 hour' AS RESULT;
  result  
----------
 03:30:00
(1 row)

/

用时间间隔除以数量,获取一段时间中的某一段:

1
2
3
4
5
SELECT interval '1 hour' / double precision '1.5' AS RESULT;
  result  
----------
 00:40:00
(1 row)