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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.