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

oracle时间处理问题方法

1、按周分割时间:
给定一个时间段,比如2012-03-01(周4) 至2011-04-17(周二)
结果:
2012-03-01至2012-03-04  
2012-03-05至2012-03-11 
2012-03-12至2012-03-18
 ....
2012-04-16(周一)至2012-04-17
2、按月分割时间:
给定一个时间段,比如2012-03-07 至2011-08-17
结果:
2012-03-07至2012-03-31
2012-04-01至2012-04-30
...
2012-08-01至2012-08-17
求好的解决方法,不胜感激!!

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


select min(sdate) m_date,max(sdate) a_date,'第'||to_char(sdate,'ww')||'周' t_num from 
(
           select date'2012-01-01'+rownum as sdate from dual
            connect by rownum < 366
)
where sdate>=date'2012-03-01' and sdate<=date'2012-04-17'
group by  to_char(sdate,'ww')
order by t_num;

          m_date      a_date        t_num
----------------------------------------------
1    2012/3/1    2012/3/3    第09周
2    2012/3/4    2012/3/10    第10周
3    2012/3/11    2012/3/17    第11周
4    2012/3/18    2012/3/24    第12周
5    2012/3/25    2012/3/31    第13周
6    2012/4/1    2012/4/7    第14周
7    2012/4/8    2012/4/14    第15周
8    2012/4/15    2012/4/17    第16周


select min(sdate) m_date,max(sdate) a_date,'第'||to_char(sdate,'mm')||'月' sdate from 
(
           select date'2012-01-01'+rownum as sdate from dual
            connect by rownum < 366
)
where sdate>=date'2012-03-07' and sdate<=date'2012-08-17'
group by  to_char(sdate,'mm')
order by t_num


          m_date      a_date        t_num
----------------------------------------------
1    2012/3/7    2012/3/31    第03月
2    2012/4/1    2012/4/30    第04月
3    2012/5/1    2012/5/31    第05月
4    2012/6/1    2012/6/30    第06月
5    2012/7/1    2012/7/31    第07月
6    2012/8/1    2012/8/17    第08月