"景先生毕设|www.jxszl.com

oracle 根据生日获取年龄函数

2023-09-12 15:40编辑: www.jxszl.com景先生毕设

oracle 根据生日获取年龄函数

、create or replace function fun_get_Age (Brithday in date) return  varchar2 is
/*-------------------------------------------------------------------------
 
|| 函数名称 :取年龄
 
|| 功能描述 :返回年龄字符串
 
||-------------------------------------------------------------------------*/
iDay integer;
iMonth integer;
iYear integer;
 
  --strReturn varchar2
 
begin
 
  iDay := trunc(sysdate) - trunc(Brithday);
 
  if iDay<0 then
 
    return ''; --如果小于当前日期,返回空字符串
 
  else
 
    iYear := floor(iDay/365);
 
    iMonth := floor((iDay - iYear*365)/30);
 
    iDay := iDay - iYear*365 -iMonth * 30;
 
    if iYear<=0 then
 
      if(iMonth<=0) then
 
        return iDay || '天'; --如果小于一个月,返回天数
      else
        return iMonth || '月' || iDay || '天' ; --如果小于一岁大于一个月,返回几月几日
      end if;
    else
      if(iYear<1) then
 
        if(iMonth<=0) then
          return iYear || '岁' || iDay || '天';
        else
          return  iYear || '岁' || iMonth || '月';
        end if;
      else
        return  iYear || '岁';
      end if;
    end if;
  end if;
end ;
================================================
================================================
举例:
select fun_get_Age(to_date('2018-01-01','yyyy-mm-dd')) from dual -- 输出: 10月14天
select fun_get_Age(to_date('1990-01-01','yyyy-mm-dd')) from dual -- 输出: 28岁
http://www.itemperor.com/a/SQL/301.html


原文链接:http://www.jxszl.com/biancheng/shujuku/445693.html