+
|
date类型参数与integer参数相加,获取时间间隔为7天后的时间:
|
SELECT date '2001-09-28' + integer '7' AS RESULT;
result
---------------------
2001-10-05 00:00:00
(1 row)
|
|
date类型参数与interval参数相加,获取时间间隔为1小时后的时间:
|
SELECT date '2001-09-28' + interval '1 hour' AS RESULT;
result
---------------------
2001-09-28 01:00:00
(1 row)
|
|
date类型参数与interval参数相加,获取时间间隔为1个月的时间:
date函数对于日期相加减超过月份的日期范围,会对齐到对应月份最后一天,不超过则不处理。
|
SELECT date '2021-01-31' + interval '1 month' AS RESULT;
result
---------------------
2021-02-28 00:00:00
(1 row)
|
|
SELECT date '2021-02-28' + interval '1 month' AS RESULT;
result
---------------------
2021-03-28 00:00:00
(1 row)
|
|
date类型参数与time类型参数相加,获取具体的日期和时间结果:
|
SELECT date '2001-09-28' + time '03:00' AS RESULT;
result
---------------------
2001-09-28 03:00:00
(1 row)
|
|
interval参数相加,获取两个时间间隔之和:
|
SELECT interval '1 day' + interval '1 hour' AS RESULT;
result
----------------
1 day 01:00:00
(1 row)
|
|
timestamp时间类型参数与interval参数相加,获取间隔23小时后的时间:
|
SELECT timestamp '2001-09-28 01:00' + interval '23 hours' AS RESULT;
result
---------------------
2001-09-29 00:00:00
(1 row)
|
|
time类型参数与interval参数相加,获取间隔时间为3小时后的时间:
|
SELECT time '01:00' + interval '3 hours' AS RESULT;
result
----------
04:00:00
(1 row)
|
|
- |
date类型参数相减,获取两个日期的时间差:
|
SELECT date '2001-10-01' - date '2001-09-28' AS RESULT;
result
--------
3 days
(1 row)
|
|
date类型参数与integer参数相减,返回timestamp类型,获取两者的时间差:
|
SELECT date '2001-10-01' - integer '7' AS RESULT;
result
---------------------
2001-09-24 00:00:00
(1 row)
|
|
date类型参数与interval参数相减,获取两者的日期、时间差:
|
SELECT date '2001-09-28' - interval '1 hour' AS RESULT;
result
---------------------
2001-09-27 23:00:00
(1 row)
|
|
time类型参数相减,获取两参数的时间差:
|
SELECT time '05:00' - time '03:00' AS RESULT;
result
----------
02:00:00
(1 row)
|
|
time类型参数与interval相减,获取两参数的时间差:
|
SELECT time '05:00' - interval '2 hours' AS RESULT;
result
----------
03:00:00
(1 row)
|
|
timestamp类型参数与interval相减,从时间戳中减去时间间隔,获取两者的日期时间差:
|
SELECT timestamp '2001-09-28 23:00' - interval '23 hours' AS RESULT;
result
---------------------
2001-09-28 00:00:00
(1 row)
|
|
interval参数相减,获取两者的时间差:
|
SELECT interval '1 day' - interval '1 hour' AS RESULT;
result
----------
23:00:00
(1 row)
|
|
timestamp类型参数相减,获取两者的日期时间差:
|
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' AS RESULT;
result
----------------
1 day 15:00:00
(1 row)
|
|
获取当前日期的前一天:
|
select now() - interval '1 day'AS RESULT;
result
-------------------------------
2022-08-08 01:46:15.555406+00
(1 row)
|
|
* |
将时间间隔乘以数量:
|
SELECT 900 * interval '1 second' AS RESULT;
result
----------
00:15:00
(1 row)
|
|
|
SELECT 21 * interval '1 day' AS RESULT;
result
---------
21 days
(1 row)
|
|
|
SELECT double precision '3.5' * interval '1 hour' AS RESULT;
result
----------
03:30:00
(1 row)
|
|
/ |
用时间间隔除以数量,获取一段时间中的某一段:
|
SELECT interval '1 hour' / double precision '1.5' AS RESULT;
result
----------
00:40:00
(1 row)
|
|