PostgreSQLで日付計算、時間計算の書き方

postgresql時間計算書き方 postgreSQL

今回はPostgreSQLで日付計算、時間計算の書き方をまとめました。
自分用にメモしていたものを公開します。

書き方(三つ)

select current_timestamp + ‘1 years’;
select current_timestamp + (1 ||’ years’)::interval;
select current_timestamp + (1 * interval ‘1 years’);

年の演算(年加算、年減算)

select current_timestamp + ‘1 years’;
select current_timestamp + ‘-1 years’;

月の演算(月加算、月減算)

select current_timestamp + ‘1 months’;
select current_timestamp + ‘-1 months’;

週の演算(週加算、週減算)

select current_timestamp + ‘1 weeks’;
select current_timestamp + ‘-1 weeks’;

日の演算(日加算、日減算)

select current_timestamp + ‘1 days’;
select current_timestamp + ‘-1 days’;

時間の演算(時間加算、時間減算)

select current_timestamp + ‘1 hours’;
select current_timestamp + ‘-1 hours’;

分の演算(分加算、分減算)

select current_timestamp + ’60 minutes’;
select current_timestamp + ‘-60 minutes’;

秒の演算(秒加算、秒減算)

select current_timestamp + ‘3600 seconds’;
select current_timestamp + ‘-3600 seconds’;
タイトルとURLをコピーしました