oralce计算2个日期间的工作日(仅排除周六周日)

参考 http://zw7534313.iteye.com/blog/609277#comments
如蓝色粗体所示,否则yk不对

CREATE OR REPLACE FUNCTION GETWORKDAYS (startdate DATE, enddate DATE)
RETURN NUMBER IS
rs INTEGER;
counts INTEGER; --两日期间的天数
weeknum INTEGER; --星期几
yunum INTEGER; --余数
yk INTEGER; --余数中有几天周末

BEGIN
  SELECT FLOOR(enddate-startdate) INTO counts FROM dual; --不包括enddate这一天
  counts:=counts+1; --//包括enddate这一天(加1)
  rs:=counts;
  yk:=0;
 
  SELECT MOD(counts,7) INTO yunum FROM dual;
  SELECT TO_NUMBER(TO_CHAR(startdate,'D')) INTO weeknum FROM dual;
  IF yunum>0 THEN
     BEGIN
           CASE weeknum
           WHEN 6 THEN
              BEGIN
                  IF yunum>1 THEN yk:=2;
                  ELSE yk:=1;
                  END IF;
               END;
           WHEN 1 THEN
                yk:=1;
           ELSE
               BEGIN
                 yk:=yunum-(7-weeknum);
                 IF yk<0 THEN
                    yk:=0;
       ELSE
  yk:=2;

                 END IF;
               END;
       END CASE;
      END;
     END IF;

  rs:=rs-FLOOR(rs/7) *2-yk; --//注意integer 类型的rs/7

  RETURN rs;

  EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GETWORKDAYS;

你可能感兴趣的:(工作,Blog)