罗列出一年的星期数

来源:互联网 发布:mac 输出当前路径 编辑:程序博客网 时间:2024/04/29 09:54
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:生成当月周一作为开始,周日作为结束,展示如下

当月第几周 本年第几周      周开始         周结束---------- --------------- -------------- --------------   1222017/6/12017/6/42232017/6/52017/6/113242017/6/122017/6/184252017/6/192017/6/255262017/6/262017/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 dualconnect by to_date('20170306', 'yyyymmdd') + level - 1 < trunc(sysdate)