日期:2014-05-17 浏览次数:20830 次
select trunc(next_day(sysdate,1),'mm') 第一周开始, trunc(next_day(sysdate,1),'dd') 第一周结束, trunc(next_day(sysdate,1),'dd')+1 第二周开始, trunc(next_day(sysdate,1)+7) 第二周结束 from dual; 结果: 第一周开始 第一周结束 第二周开始 第二周结束 2010-05-01 2010-05-02 2010-05-03 2010-05-09
------解决方案--------------------
SQL> select week_of_year + 1 - min(week_of_year) over() week_no_of_month, 2 first_day_of_week, 3 last_day_of_week 4 from ( 5 select min(the_day) first_day_of_week, 6 max(the_day) last_day_of_week, 7 to_char(the_day, 'IW') week_of_year 8 from ( 9 --把一个月的日期都列出来 10 select start_date + rownum - 1 the_day 11 from ( 12 select to_date('201004' || '01', 'yyyymmdd') start_date, 13 add_months(to_date('201004' || '01', 'yyyymmdd'), 1) - 1 end_date 14 from dual 15 ) 16 connect by (start_date + rownum - 1) <= end_date 17 ) t 18 group by to_char(the_day, 'IW') 19 ) 20 / WEEK_NO_OF_MONTH FIRST_DAY_OF_WEEK LAST_DAY_OF_WEEK ---------------- ----------------- ---------------- 1 2010/4/1 2010/4/4 2 2010/4/5 2010/4/11 3 2010/4/12 2010/4/18 4 2010/4/19 2010/4/25 5 2010/4/26 2010/4/30
------解决方案--------------------
判断一个月有几周的函数
CREATE OR REPLACE FUNCTION weeknumOfMonth(p_month IN VARCHAR2) RETURN NUMBER AS weeknum NUMBER; begin SELECT count(DISTINCT to_char(to_date(p_month,'yyyymm')+ROWNUM-1,'iw')) into weeknum FROM dual CONNECT BY ROWNUM<=add_months(to_date(p_month,'yyyymm'),1)-to_date(p_month,'yyyymm'); RETURN weeknum; exception WHEN others THEN RETURN -1; END;
------解决方案--------------------
CREATE OR REPLACE PACKAGE SP_WEEK IS
-- 功能 : 一个月有几周,开始时间和结束时间
TYPE CUR IS REF CURSOR;
PROCEDURE SP_WEEK(P_YEARMONTH VARCHAR2,
C OUT CUR);
END SP_WEEK;
CREATE OR REPLACE PACKAGE BODY SP_WEEK IS
PROCEDURE SP_WEEK( P_YEARMONTH VARCHAR2,
C OUT CUR) IS
BEGIN
IF LENGTH(P_YEARMONTH)=6 THEN
OPEN C FOR
SELECT week_of_year + 1 - min(week_of_year) over() week_no_of_month,
first_day_of_week,
last_day_of_week
from (
select min(the_day) first_day_of_week,
max(the_day) last_day_of_week,
to_char(the_day, 'IW') week_of_year
from (
--把一个月的日期都列出来
select start_date + rownum - 1 the_day
from (
select to_date(P_YEARMONTH || '01', 'yyyymmdd') start_date,