Home  >  Article  >  Database  >  Oracle存储过程根据指定日期返回(N个)工作日的时间

Oracle存储过程根据指定日期返回(N个)工作日的时间

WBOY
WBOYOriginal
2016-06-07 16:00:231651browse

一直都没写过Oracle的存储过程,今天突然来了一个需求:计算指定日期的前N个工作日或者后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        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        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;

本文永久更新链接地址

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn