Oracle預存程序根據指定日期返回(N個)工作日的時間

來源:互聯網
上載者:User

Oracle預存程序根據指定日期返回(N個)工作日的時間

一直都沒寫過Oracle的預存程序,今天突然來了一個需求:計算指定日期的前N個工作日或者後N個工作日日期(去除周末,法定節假日無法計算),然後研究了一下 Oracle的時間函數和迴圈方法。具體實現方法如下,也沒啥難的,對資料庫沒研究過,也不知道下面的寫法效率怎麼樣。

或者有沒有更好的寫法。o(︶︿︶)o 唉!

create or replace procedure proc_CalculationWorkDate
(
  plan_date in date,--登入日期
  flag in number,--1 往前日期,0往後日期
  date_number in number,--天數
  out_date out date--計算出的日期
)
is
  dayOfWeek number:=0;--星期的數字
  dates number:=date_number;--往前或者往後的天數(包含工作日的)初始化給他等於天數
  i int:=0;
  j int:=0;
begin
  if flag=1 then--計算往前日期
      while i<date_number+1 loop
        SELECT to_number(to_char(sysdate+i+j,'D')) into dayOfWeek  FROM DUAL;--返回星期代表的數值
        if dayOfWeek=1 or dayOfWeek=7 then --周六 周日
          dates:=dates+1;
          i:=i;
          j:=j+1;
        else
          i:=i+1;
        end if;
      end loop;
      select plan_date+dates into out_date from dual;
      --DBMS_OUTPUT.PUT_LINE(dates);
  end if;
  if flag=0 then --計算往後日期
      while i<date_number+1 loop
        SELECT to_number(to_char(sysdate-i-j,'D')) into dayOfWeek  FROM DUAL;
        if dayOfWeek=1 or dayOfWeek=7 then
          dates:=dates+1;
          i:=i;
          j:=j+1;
        else
          i:=i+1;
        end if;
      end loop;
      select plan_date-dates into out_date from dual;
      --DBMS_OUTPUT.PUT_LINE(dates);
  end if;
end;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.