首页 > 数据库 >Oracle计算对公天数

Oracle计算对公天数

时间:2023-02-25 10:13:13浏览次数:40  
标签:iMonth end 对公 天数 number date str iDay Oracle

  create  or replace function calTwoDays(startDate in date,endDate in date)
  return number
  is 
    dtStaDat date;
    dtEndDat date;
    
    start_date_str varchar2(100);
    end_date_str varchar2(100);
    
    iStaYear number;
    iStaMonth number;
    iStaDay number;
    
    iEndYear number;
    iEndMonth number;
    iEndDay number;
    
    iYear number;
    iMonth number;
    iMonthStr varchar2(4);
    iDay number;
    iDayStr varchar2(4);
    
    lDay1 number;
    lDay2 number;
    lDays number;
    gcldTmp date;
  begin
    -- Test statements here
    dtStaDat := startDate;
    dtEndDat := endDate;
    
    start_date_str:= to_char(startDate,'YYYYMMDD'); 
    end_date_str  := to_char(endDate,'YYYYMMDD');
    
    iStaYear   := to_number(substr(start_date_str,1,4));
    iStaMonth  := to_number(substr(start_date_str,5,2));
    iStaDay    := to_number(substr(start_date_str,7,2));
    
    iEndYear   := to_number(substr(end_date_str,1,4));
    iEndMonth  := to_number(substr(end_date_str,5,2));
    iEndDay    := to_number(substr(end_date_str,7,2));
    
    iYear  := iEndYear;
    iMonth := iEndMonth;
    iDay   := iStaDay;
    
    if 4 = iMonth or 6 = iMonth or 9 = iMonth or 11 = iMonth then
       if iDay > 30 then
          iDay := 30;
       end if;
    elsif 2 = iMonth then
      --闰年
      if '29' = to_char(last_day(to_date(iYear||'0201','YYYYMMDD')),'DD') then 
         if iDay > 29 then
            iDay := 29;
         end if;
      else   
         if iDay > 28 then
            iDay := 28;
         end if;          
      end if;
    end if;
    
    if iDay > iEndDay then
      iYear := iEndYear;
      iMonth := iEndMonth - 1;
      iDay := iStaDay;
      
      if 0 = iMonth then
          iMonth := 12;
          iYear := iYear-1;
      end if;
      
      if 4 = iMonth or 6 = iMonth or 9 = iMonth or 11 = iMonth then
          if iDay > 30 then
            iDay := 30;
          end if;
      elsif 2 = iMonth then
         --闰年
        if '29' = to_char(last_day(to_date(iYear||'0201','YYYYMMDD')),'DD') then 
            if iDay > 29 then
              iDay := 29;
            end if;
         else 
            if iDay > 28 then
              iDay := 28;
            end if;
         end if;
       end if;  
     end if;
     
     lDay1 := (iYear - iStaYear) * 360 + (iMonth - iStaMonth) * 30;
     if iMonth < 10 then
       iMonthStr := '0'||iMonth;
     else
       iMonthStr :=''||iMonth;
     end if;
     if iDay < 10 then
       iDayStr := '0'||iDay;
     else
       iDayStr :=''||iDay;  
     end if;
     
     gcldTmp := to_date(iYear||iMonthStr||iDayStr,'YYYYMMDD');
     
     if dtEndDat > gcldTmp then
       lDay2 := dtEndDat - gcldTmp;
     else 
       lDay2 := gcldTmp - dtEndDat;  
     end if;
     
     lDays := lDay1 + lDay2;
     
     return lDays;
  end;


标签:iMonth,end,对公,天数,number,date,str,iDay,Oracle
From: https://www.cnblogs.com/gsqc/p/17153829.html

相关文章

  • oracle:查看sql执行计划 explain PLAN FOR
    例如:explainPLANFORselect*from(selectoti.*frommb_order_ticket_itemotiwhereoti.ticket_idin(selectot.idfrommb_order_ticketotwhereot.ti......
  • 使用 PL/SQL Developer Version 14 调试 Oracle 存储过程
    1、选中当前存储过程,右击后弹出上下文菜单,点击“测试”(如下图):2、编辑目标存储过程,在需要关注的语句行号前右击鼠标设置断点,如下图:3、修改调试窗口的代码为代码块中初始化......
  • linux下oracle自动备份
    备份脚本1vi/backup/oracledata/ordatabak.sh#!/bin/bashexportORACLE_BASE=/u01/oracleexportORACLE_HOME=$ORACLE_BASE/oracleexportORACLE_SID=orclexportPATH=......
  • Oracle数据库之用户角色管理(一)
                          Oracle数据库之用户角色管理(一)关系型数据库通常指的是数据之间的关系,例如主键约束,外键约束等等这些,这......
  • Oracle数据库基本操作(和MySQL的类比操作)全局查询篇
                             Oracle数据库基本操作(和MySQL的类比操作)全局查询篇Oracle数据库和MySQL数据库同属关系型数......
  • [DB] Oracle 表空间达到 32GB 后扩容的方法
    背景Oracle表空间数据文件容量与DB_BLOCK_SIZE参数有关,物理文件最大允许4194304个数据块(由操作系统决定)。如果初始建库时DB_BLOCK_SIZE=8K,数据文件最大体积为32G......
  • Oracle数据库1
    一.对比Mysql,理解Oracle的基本概念1.1.Oracle数据库的基本概念问题1:什么是数据库?1.数据库:有组织的数据的集合。俗称「数据的仓库」。2.数据库系统分为数据库管理系统......
  • oracle order by 和 rownum<1结果失败问题解决并且 fetch first很好的解决
    #在oracle中如下所示SQL>selectmax(sequence#)fromv$archived_logorderbysequence#;MAX(SEQUENCE#)--------------193#使用如下字句查询时候SQL>......
  • oracle dgbroke异常ora-16810
    1.查看状态如下信息2.两个关键命令showdatabase'sbdb'statusreport;showdatabaseverboseSBDB;3.查看状态信息showdatabase'jkzy'statusreport;   ......
  • Oracle数据库迁移
    准备工作:原库上定位dmp文件导出路径:$ORACLE_HOME/oradata/dump新库的存放dmp文件的路径:$ORACLE_HOME/oradata/dump整理需要迁移的表空间:dbuser/password按照用户模式导......