日期:2014-05-17  浏览次数:20824 次

求Oracle的sql
给定某一年(如2011),该年的第几周(如20周),求一个SQL,已知年和周,得到起止时间


------解决方案--------------------
SQL code

select * from (
select to_char(to_date('2011'||'-01', 'YYYY-MM') + rownum - 1, 'YYYY-MM-DD') sdate 
from dual
connect by rownum <= 366)
where to_char(to_date(sdate,'yyyy-mm-dd'),'WW')=20
           sdate 
---------------------
1    2011-05-14
2    2011-05-15
3    2011-05-16
4    2011-05-17
5    2011-05-18
6    2011-05-19
7    2011-05-20

------解决方案--------------------
上面是根据年和周获取那周的日期

这个是获取周的起始日期
SQL code

select max(sdate) m_date,min(sdate) n_date from (
select to_char(to_date('2011'||'-01', 'YYYY-MM') + rownum - 1, 'YYYY-MM-DD') as sdate 
from dual
connect by rownum <= 365)
where to_char(to_date(sdate,'yyyy-mm-dd'),'WW')=20

         m_date          n_date
-------------------------------------
1    2011-05-20    2011-05-14