[分享]周别相关SQL
洪十二 2004-08-18 04:00:13 1.查询某周的第一天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') last_day
from (select substr('2004-32', 1, 4) yy, to_number(substr('2004-32', 6)) ww
from dual)
select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d')-6 first_day from dual
select min(v_date) from
(select (to_date('200201','yyyymm') + rownum) v_date
from all_tables
where rownum < 370)
where to_char(v_date,'yyyy-iw') = '2002-49'
2.查询某周的最后一天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') - 6 first_day
from (select substr('2004-33', 1, 4) yy, to_number(substr('2004-33', 6)) ww
from dual)
select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d') last_day from dual
select max(v_date) from
(select (to_date('200408','yyyymm') + rownum) v_date
from all_tables
where rownum < 370)
where to_char(v_date,'yyyy-iw') = '2004-33'
3.查询某周的日期
select min_date, to_char(min_date,'day') day from
(select to_date(substr('2004-33',1,4)||'001'+rownum-1,'yyyyddd') min_date
from all_tables
where rownum <= decode(mod(to_number(substr('2004-33',1,4)),4),0,366,365)
union
select to_date(substr('2004-33',1,4)-1||
decode(mod(to_number(substr('2004-33',1,4))-1,4),0,359,358)+rownum,'yyyyddd') min_date
from all_tables
where rownum <= 7
union
select to_date(substr('2004-33',1,4)+1||'001'+rownum-1,'yyyyddd') min_date
from all_tables
where rownum <= 7
)
where to_char(min_date,'yyyy-iw') ='2004-33'