SAK 図書館
PostgreSQL 編16 - 日付計算、曜日、月末日、時間計算、期間計算、日付抽出
■年の演算(年加算、年減算)
・年の演算を行うには、次のようにする。
select current_timestamp + '1 years';
?column?
-----------------------------
2003-11-03 17:26:35.4905+09
select current_timestamp + '-1 years';
?column?
-----------------------------
2001-11-03 17:27:12.8085+09
■月の演算(月加算、月減算)
・月の演算を行うには、次のようにする。
select current_timestamp + '1 months';
?column?
-----------------------------
2002-12-03 17:28:15.5505+09
select current_timestamp + '-1 months';
?column?
-----------------------------
2002-10-03 17:28:29.1925+09
■曜日の演算(曜日計算)
・次の週を計算するには、、次のようにする。
前の週を計算するには、更に -7 すれば良い。
これは、もっとシンプルな数式で実現できるかも。
3 が求める曜日(ここでは 3 水曜日) である。
select
case
when 3 - date_part('dow', current_timestamp) < 0 then current_timestamp + 3 + 7 - date_part('dow', current_timestamp)
else current_timestamp + 3 - date_part('dow', current_timestamp)
end
;
case
------------
2002-11-06
■週の演算(週加算、週減算)
・週の演算を行うには、次のようにする。
select current_timestamp + '1 weeks';
?column?
-----------------------------
2002-11-10 18:19:03.7065+09
select current_timestamp + '-1 weeks';
?column?
-----------------------------
2002-10-27 18:19:13.7865+09
■月末日の演算
・月末日を求めるには、次のようにする。
これも、もっとシンプルな方法がありそうである。
select
case date_part('month', current_timestamp)
when 1 then 31
when 2 then
case date_part('doy', to_date(to_char(current_timestamp, 'yyyy-12-31'), 'yyyy-mm-dd'))
when 365 then 28
else 29
end
when 3 then 31
when 4 then 30
when 5 then 31
when 6 then 30
when 7 then 31
when 8 then 31
when 9 then 30
when 10 then 31
when 11 then 30
when 12 then 31
end
;
case
------
30
■日の演算(日加算、日減算)
・うるう年や大の月、小の月も考えなくて良い。
select current_timestamp + '1 days';
?column?
-----------------------------
2002-11-04 18:40:37.4885+09
select current_timestamp + '-1 days';
?column?
-----------------------------
2002-11-02 18:40:48.1375+09
■日の 0 時
・0 時に切り捨てるには、次のようにする。
select date_trunc('day', current_timestamp);
date_trunc
------------------------
2002-11-03 00:00:00+09
■時間の演算(時間加算、時間減算)
・date 型、小数部で時間を管理している。
select current_timestamp + '1 hours';
?column?
-----------------------------
2002-11-03 19:42:31.8685+09
select current_timestamp + '-1 hours';
?column?
-----------------------------
2002-11-03 17:42:41.8175+09
■分の演算(分加算、分減算)
・date 型、小数部で時間を管理しているので、24 * 60 = 1440 が分の単位と
なる。
select current_timestamp + '60 minutes';
?column?
-----------------------------
2002-11-03 19:43:29.5455+09
select current_timestamp + '-60 minutes';
?column?
-----------------------------
2002-11-03 17:43:40.3245+09
■秒の演算(秒加算、秒減算)
・date 型、小数部で時間を管理しているので、1440 * 60 = 86400 が秒の
単位となる。
select current_timestamp + '3600 seconds';
?column?
-----------------------------
2002-11-03 19:44:17.7585+09
select current_timestamp + '-3600 seconds';
?column?
-----------------------------
2002-11-03 17:44:28.1725+09
■日付間隔、時間間隔、期間計算、時間計算、経過日数、期間内日数
・日付や時間の間隔を計算するには、次のようにする。
select current_timestamp - date '2001-01-01';
?column?
------------------------------
671 days 18:45:51.3175000027
・経過年数計算、勤続年数計算、年齢計算は、age を使用する。
select age(current_timestamp, date '2001-01-01');
age
-------------------------------------
1 year 10 mons 2 days 18:47:03.1305
■日付抽出、日付比較、日付判定、時間抽出、時間比較、時間判定
・日付の範囲や時間の範囲でレコードを抽出するには、次のようにする。
日付に索引が付けてあるかにもよるが、一般的にも like より、日付として
の抽出の方が高速と思う。
(Date 型比較、日付型比較、日付大小比較、日付範囲指定)
(日付データ抽出、日付検索、期間指定、期間範囲、期間集計、期間検索)
create table test3m (
key char(8),
data1 int8,
jcdt timestamp
);
insert into test3m values ('a001', 1, '2001-01-01');
insert into test3m values ('a001', 1, '2002-01-01');
insert into test3m values ('a001', 1, '2002-06-01');
insert into test3m values ('a001', 1, '2002-12-01');
insert into test3m values ('a001', 1, '2003-01-01');
select * from test3m
where jcdt between date '2002-01-01' and date '2002-12-31';
select * from test3m
where jcdt between '2002-01-01' and '2002-12-31';
key | data1 | jcdt
----------+-------+------------------------
a001 | 1 | 2002-01-01 00:00:00+09
a001 | 1 | 2002-06-01 00:00:00+09
a001 | 1 | 2002-12-01 00:00:00+09
select * from test3m
where jcdt >= '2002-01-01';
key | data1 | jcdt
----------+-------+------------------------
a001 | 1 | 2002-01-01 00:00:00+09
a001 | 1 | 2002-06-01 00:00:00+09
a001 | 1 | 2002-12-01 00:00:00+09
a001 | 1 | 2003-01-01 00:00:00+09
select * from test3m
where date_trunc('month', jcdt) = '2002-06-01';
key | data1 | jcdt
----------+-------+------------------------
a001 | 1 | 2002-06-01 00:00:00+09
select * from test3m
where to_char(jcdt, 'yyyy.mm.dd hh24:mi:ss') like '2001%';
key | data1 | jcdt
----------+-------+------------------------
a001 | 1 | 2001-01-01 00:00:00+09
■PostgreSQL 編、JAVA Servlet、JSP 編資料
■MySQL 編資料
■SQL 基礎編資料
■SQL 基礎実地編資料
■SQL チューニング編資料
■Oracle PL/SQL 編資料