How to populate calendar table in Oracle?

前端 未结 3 1867
耶瑟儿~
耶瑟儿~ 2020-11-28 16:06

I want to maintain a calender table in Oracle DB which I want to populate with all the days of the year starting from 2011 to 2013 (it may be till any year). How can I do th

相关标签:
3条回答
  • 2020-11-28 16:28

    This is a simple and easy way to do it

    with calendar as (
            select :startdate + rownum - 1 as day
            from dual
            connect by rownum < :enddate - :startdate
        )
    select rownum as "S.No", to_date(day,'dd_mm_yyyy') as "Cal_Dt", to_char(day,'day') as "DayName"
    from calendar
    
    0 讨论(0)
  • 2020-11-28 16:30
    declare
      v_date date := to_date('20110101','yyyymmdd');
    begin
    
       while v_date < sysdate + 720 loop
    
          insert into calender
          values ( v_date, to_char(v_date,'DAY'));
    
          v_date := v_date + 1;
    
       end loop;
       commit;
    
    end;
    /
    

    This is not best practice and you should use Allesandro Rossi's solution. This may only be useful if you're using Oracle 9i or earlier and populating a large table.

    0 讨论(0)
  • 2020-11-28 16:35
    with calendar as (
            select rownum - 1 as daynum
            from dual
            connect by rownum < sysdate - to_date('1-jan-2010') + 1
        )
    select to_date('1-jan-2010') + daynum as monthdate
    from calendar
    ;
    
    0 讨论(0)
提交回复
热议问题