PostgreSQl 13に日付の秒の加算サンプル

環境
Windows10 64 bit
PostgreSQL 13.2

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

使用例
1.現在時刻に15秒加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select now() + cast('15 seconds' as INTERVAL) result;
result
------------------------------
2022-01-09 08:44:54.31799+09
(1)
postgres=# select now() + cast('15 seconds' as INTERVAL) result; result ------------------------------ 2022-01-09 08:44:54.31799+09 (1 行)
postgres=# select now() + cast('15 seconds' as INTERVAL) result;
            result
------------------------------
 2022-01-09 08:44:54.31799+09
(1 行)

2.「2022/1/10」に15秒加えます

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

3.「2022/1/10 14:14:00」に15秒加えます

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast('20220110 14:14:00' as timestamp) + cast('15 seconds' as INTERVAL) result;
result
---------------------
2022-01-10 14:14:15
(1)
postgres=# select cast('20220110 14:14:00' as timestamp) + cast('15 seconds' as INTERVAL) result; result --------------------- 2022-01-10 14:14:15 (1 行)
postgres=# select cast('20220110 14:14:00' as timestamp) + cast('15 seconds' as INTERVAL) result;
       result
---------------------
 2022-01-10 14:14:15
(1 行)

 

PostgreSQL

Posted by arkgame