0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

マスターカレンダー作成用SQL(Oracle Database)

Last updated at Posted at 2019-10-22

マスターカレンダー作成

Oracle Database用

select
     a.calendar                            as mcal_date
    ,to_char(a.calendar,'YYYY') as mcal_yyyy -- 年:YYYY
    ,to_char(a.calendar,'MM'  ) as mcal_mm   -- 月:MM
    ,to_char(a.calendar,'YYYY')||to_char(a.calendar,'MM'  ) as mcal_yyyymm -- 年月:YYYYMM
    ,to_char(a.calendar,'DD'  ) as mcal_dd   -- 日:DD
    ,case when to_number(to_char(a.calendar,'MM')) < 4 then 
        to_number(to_char(a.calendar,'YYYY')) - 1
     else
        to_number(to_char(a.calendar,'YYYY'))
     end as mcal_nendo                                  -- 年度:YYYY
    ,case when to_number(to_char(a.calendar,'MM')) < 4 then
        to_number(to_char(a.calendar,'MM')) + 9
    else
        to_number(to_char(a.calendar,'MM')) -3
    end as mcal_mm_sort                                -- 月ソート順
    ,to_char(a.calendar, 'DY') as mcal_dy              -- 曜日(漢字)
    ,to_number(to_char(a.calendar, 'D')) as mcal_d     -- 曜日(数値)
from
(
    select
        to_date('20010401', 'YYYYMMDD') + rownum - 1 as calendar --- 起点を2001年04月01日とする
    from
        (select rownum as "dummy" from dual connect by rownum <= 36500) -- 100年分のダミーレコード。それより長くする場合は要調整
    where
        to_date('20010401', 'YYYYMMDD') + rownum - 1 <= to_date('20210331', 'YYYYMMDD') -- 範囲を2001年04月01日~2021年03月31日とする
    order by
        1
) a;

参考リンク

Oracleで連続する日付データ(カレンダー)を作成する
Oracle でテーブルやビューを使わずに任意の複数行のデータを取得したい

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?