Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate calendar table in Oracle?

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 that?

Consider my DB table has columns and example dataset is:

S.No  Cal_Dt      DayName 
1     01-01-2011  Monday
2     02-01-2011  Tuesday
3     03-01-2011  Wednesday

and so on.

I am more concerned with the Cal_Dt only here (DayName is optional).

like image 619
terrific Avatar asked Dec 04 '11 11:12

terrific


1 Answers

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
like image 82
Alessandro Rossi Avatar answered Oct 08 '22 14:10

Alessandro Rossi