PostgreSQL 13に日付の分の加算のサンプル

環境
Windows10 64 bit
PostgreSQL 13.2

書式
時間の加算
日付 + cast( 'xx minutes’ as INTERVAL )

使用例
1.現在時刻に30分加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select now() + cast('30 minutes' as INTERVAL) result;
result
-----------------------------
2022-01-09 09:04:55.5599+09
(1)
postgres=# select now() + cast('30 minutes' as INTERVAL) result; result ----------------------------- 2022-01-09 09:04:55.5599+09 (1 行)
postgres=# select now() + cast('30 minutes' as INTERVAL) result;
           result
-----------------------------
 2022-01-09 09:04:55.5599+09
(1 行)

2.「2022/1/10」に30分加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast('2022/1/10' as date) + cast('30 minutes' as INTERVAL) result;
result
---------------------
2022-01-10 00:30:00
(1)
postgres=# select cast('2022/1/10' as date) + cast('30 minutes' as INTERVAL) result; result --------------------- 2022-01-10 00:30:00 (1 行)
postgres=# select cast('2022/1/10' as date) + cast('30 minutes' as INTERVAL) result;
       result
---------------------
 2022-01-10 00:30:00
(1 行)

3.「2022/1/9 8:40:10」に30分加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast('20220109 08:40:10' as timestamp) + cast('30 minutes' as INTERVAL) result;
result
---------------------
2022-01-09 09:10:10
(1)
postgres=# select cast('20220109 08:40:10' as timestamp) + cast('30 minutes' as INTERVAL) result; result --------------------- 2022-01-09 09:10:10 (1 行)
postgres=# select cast('20220109 08:40:10' as timestamp) + cast('30 minutes' as INTERVAL) result;
       result
---------------------
 2022-01-09 09:10:10
(1 行)

 

PostgreSQL

Posted by arkgame