ORACLE函数创建,日期加减函数,要区分工作日与自然日(自然日类型不考虑返回的 日期是否处在节假日的问题)

create or replace function func_getLimitDate(in_date date, --输入日期
                                             d_type  varchar2, --类型。G:工作日 , Z :自然日,2:小时,3:分钟
                                             step    number --加上的天数,为负值表示减
                                             --修改为加上的时间,如果是工作日,就是表示加上 step 个工作日;若果是分钟,就是表示加上 step 个分钟
                                             ) return date IS
  /************************************************************
    用途:日期加减函数,要区分工作日与自然日(自然日类型不考虑返回的
          日期是否处在节假日的问题)
    作者:vigo
    创建日期:2004-9-15
    修改人: 增加了时限单位为自然小时、自然分钟的情况(不考虑工作小时、工作分钟的情况)
    修改日期:2006-3-15
    依赖:t_sys_holiday
  ************************************************************/
  Result      date;
  v_tempdate  date;
  v_step      number(4, 0);
  v_year      varchar2(4);
  v_month     varchar2(4);
  v_day       varchar2(4);
  v_isHoliday char;
  Datetype    int default 3;
begin
  if d_type = 'G' then
    
    Datetype := 0;
  end if;
  if d_type = 'Z' then
    Datetype := 1;
  end if;
  if step = 0 then
    --加减0天,直接返回
    v_tempdate := in_date;
    v_year     := to_char(v_tempdate, 'YYYY');
    v_month    := to_number(to_char(v_tempdate, 'MM'));
    v_day      := to_char(v_tempdate, 'DD');
    select ISHOLIDAY
      into v_isHoliday
      from t_sys_holiday
     where year = v_year
       and month = v_month
       and day = v_day;
    if v_isHoliday = '0' then
      return(in_date);
    elsif v_isHoliday = '1' then
      v_step := 1;
      while v_step > 0 loop
        if v_step > 0 then
          v_tempdate := v_tempdate + 1;
        else
          v_tempdate := v_tempdate - 1;
        end if;
        v_year  := to_char(v_tempdate, 'YYYY');
        v_month := to_number(to_char(v_tempdate, 'MM'));
        v_day   := to_char(v_tempdate, 'DD');
        begin
          v_isHoliday := null;
          select ISHOLIDAY
            into v_isHoliday
            from t_sys_holiday
           where isholiday = '0'
             and year = v_year
             and month = v_month
             and day = v_day;
        exception
          when no_data_found then
            --不是工作日
            v_step := v_step + 1;
        end;
        v_step := v_step - 1;
      end loop;
      Result := v_tempdate;
    end if;
  elsif Datetype = 1 then
    --自然日
    return(in_date + step);
  elsif Datetype = 2 then
    --小时
    return(in_date + step / 24);
  elsif Datetype = 3 then
    --分钟
    return(in_date + step / 1440);
  else
    begin
      --工作日
      v_tempdate := in_date;
      v_step     := abs(step);
      while v_step > 0 loop
        if step > 0 then
          v_tempdate := v_tempdate + 1;
        else
          v_tempdate := v_tempdate - 1;
        end if;
        v_year  := to_char(v_tempdate, 'YYYY');
        v_month := to_number(to_char(v_tempdate, 'MM'));
        v_day   := to_char(v_tempdate, 'DD');
        begin
          v_isHoliday := null;
          select ISHOLIDAY
            into v_isHoliday
            from t_sys_holiday
           where isholiday = '0'
             and year = v_year
             and month = v_month
             and day = v_day;
        exception
          when no_data_found then
            --不是工作日
            v_step := v_step + 1;
        end;
        v_step := v_step - 1;
      end loop;
      Result := v_tempdate;
    end;
  end if;

  return(Result);
end func_getLimitDate;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值