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

環境
Windows10 64 bit
PostgreSQL 13.2

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

使用例
1.現在時刻に4時間加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select now() + cast('4 hours' as INTERVAL) result;
result
-------------------------------
2022-01-09 12:27:42.513338+09
(1)
postgres=# select now() + cast('4 hours' as INTERVAL) result; result ------------------------------- 2022-01-09 12:27:42.513338+09 (1 行)
postgres=# select now() + cast('4 hours' as INTERVAL) result;
            result
-------------------------------
 2022-01-09 12:27:42.513338+09
(1 行)

2.「2022/1/6」に4時間加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast('2020/1/6' as date) + cast('4 hours' as INTERVAL) result;
result
---------------------
2020-01-06 04:00:00
(1)
postgres=# select cast('2020/1/6' as date) + cast('4 hours' as INTERVAL) result; result --------------------- 2020-01-06 04:00:00 (1 行)
postgres=# select cast('2020/1/6' as date) + cast('4 hours' as INTERVAL) result;
       result
---------------------
 2020-01-06 04:00:00
(1 行)

3.「2022/1/6 12:00:00」に4時間加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast('20200106 07:00:00' as timestamp) + cast('4 hours' as INTERVAL) result;
result
---------------------
2020-01-06 11:00:00
(1)
postgres=# select cast('20200106 07:00:00' as timestamp) + cast('4 hours' as INTERVAL) result; result --------------------- 2020-01-06 11:00:00 (1 行)
postgres=# select cast('20200106 07:00:00' as timestamp) + cast('4 hours' as INTERVAL) result;
       result
---------------------
 2020-01-06 11:00:00
(1 行)

 

PostgreSQL

Posted by arkgame