N1:
with x0 as
(select to_date('2016-01-01','yyyy-mm-dd') as 年初 from dual),
x1 as (select 年初,add_months(年初,12) as 下年初 from x0),
x2 as (select 年初,下年初,下年初-年初 as 天數 from x1),
x3 as /*生成列表*/
(select 年初+(LEVEL -1) AS 日期 from x2 connect by level <=天數),
x4 as /*對數據進行轉換*/
(select 日期,to_char(日期,'DY') as 星期 from x3)
select * from x4
N2:生成當月週一作爲開始,週日作爲結束,展示如下
當月第幾周 本年第幾周 周開始 周結束
---------- --------------- -------------- --------------
1 22 2017/6/1 2017/6/4
2 23 2017/6/5 2017/6/11
3 24 2017/6/12 2017/6/18
4 25 2017/6/19 2017/6/25
5 26 2017/6/26 2017/6/30
with t1 as
(select trunc(sysdate, 'mm') start_month,
add_months(trunc(sysdate, 'mm'), 1) - 1 end_month
from dual)
t2 as
(select start_month + level - 1 months_day,
to_char(start_month + level - 1, 'iw') locate_week
from t1
connect by level <= end_month - start_month + 1),
t3 as
(select locate_week, min(months_day) min_day, max(months_day) max_day
from t2
group by locate_week
order by 1)
select rownum "當月第幾周",
t3.locate_week "本年第幾周",
t3.min_day "周開始",
t3.max_day "周結束"
from t3;
N3:構造從20170306到當前日期的時間
select to_date('20170306', 'yyyymmdd') + level - 1
from dual
connect by to_date('20170306', 'yyyymmdd') + level - 1 < trunc(sysdate)