Calc_Date


Oracle

日付計算

期間の初め/終わりを取得

月初
select trunc(log_date, 'MONTH') from xxx;
月末
select last_day(log_date) from xxx;

週の初め(日曜日)
select next_day(log_date, '日') -7
  または、
select log_date - to_number(to_char(log_date, 'D')) + 1
週の終わり(土曜日)
select next_day(log_date, '土')
  • next_day(log_date, 'xxx')は、(その日を含まない)次の曜日。
  • to_char(log_date, 'D')は、1(Sun), 2(Mon), ..., 6(Sat)

四半期 (12月〜11月)

四半期の初め:
select add_months(trunc(log_date, 'MONTH'),
         -mod(to_number(to_char(log_date, 'MM')), 3))

日付の差

月数
select months_between(to_date, from_date) from xxx;
-> to_date - from_date (端数の日付は、小数表示(日数/31))
日数(引き算)
select to_date - from_date from ...
年数(年齢)
trunc((to_number(to_char(today, 'YYYYMMDD'))
- to_number(to_char (birth_date, 'YYYYMMDD'))
)/10000, 0) as age

西暦 - 和暦 変換

例)

case when a.BIRTH_NENGO = 'S' then to_date(to_char(to_number(a.BIRTH_WAREKI) + 19250000), 'YYYYMMDD') 
 when a.BIRTH_NENGO = 'H' then to_date(to_char(to_number(a.BIRTH_WAREKI) + 19880000), 'YYYYMMDD')
 when a.BIRTH_NENGO = 'T' then to_date(to_char(to_number(a.BIRTH_WAREKI) + 19110000), 'YYYYMMDD')
 when a.BIRTH_NENGO = 'M' then to_date(to_char(to_number(a.BIRTH_WAREKI) + 18670000), 'YYYYMMDD')
else null end as birth_date,

最新の20件

2014-11-17 2014-11-04

今日の1件

  • Calc_Date(1)

  • counter: 261
  • today: 1
  • yesterday: 0
  • online: 1