天天看點

oracle 制作月曆表,建立本月月曆,建立某一個月曆

with  t1 as

-- 給定一個日期

(select  to_date('2016-10-27','yyyy-mm-dd') as cur_date from dual),

t2 as  --  取月初

(select  trunc(cur_date,'mm') as  月初,add_months(trunc(cur_date,'mm'),1)as 下月初 from  t1),

t3 as   -- 枚舉當月所有的天

(select  月初+ (level-1) as  日  from  t2

connect by level <=(下月初-月初) ),

-- 提取周資訊

t4 as

(select  to_char(日,'iw') 所在周,

to_char(日,'dd') 日期,

to_number(to_char(日,'d')) 周幾 from  t3)

select

max(case 周幾 when  2  then  日期 end ) 周一,

max(case 周幾 when  3  then  日期 end ) 周二,

max(case 周幾 when  4  then  日期 end ) 周三,

max(case 周幾 when  5  then  日期 end ) 周四,

max(case 周幾 when  6  then  日期 end ) 周五,

max(case 周幾 when  7  then  日期 end ) 周六,

max(case 周幾 when  1  then  日期 end ) 周日

from  t4

group by 所在周

order by 所在周

oracle 制作月曆表,建立本月月曆,建立某一個月曆

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31369373/viewspace-2127225/,如需轉載,請注明出處,否則将追究法律責任。