ITPub博客

首页 > 数字化转型 > ERP > 中文年龄函数

中文年龄函数

原创 ERP 作者:xypincle 时间:2017-02-19 21:31:46 0 删除 编辑

  1. create or replace function CHINESEAGE(BirthDate in date, NowDate in date) return string is
  2.       Result string(80); -- 返回值
  3.       MonthCount number(4,0); -- 出生的月份
  4.       DayCount number(6,0); -- 去除月份后的余数(天)
  5. begin
  6.       -- 如果生日大于当前日期,则提示错误
  7.       if BirthDate > NowDate or BirthDate is null or NowDate is null Then
  8.             Result := '不详';
  9.             return(Result);
  10.       end if;

  11.       -- 获取出生多少个月余多少天
  12.       SELECT trunc(Mon), trunc((Mon - trunc(Mon)) * 31)
  13.       INTO MonthCount,DayCount
  14.       FROM (SELECT months_between(NowDate, BirthDate) Mon FROM dual) V;

  15.       -- 如果出生60个月以上,年龄显示为 ##岁
  16.       if MonthCount >= 60 then
  17.             Result := to_char(trunc(MonthCount/12))||'岁';

  18.       -- 如果出生12-59个月,年龄显示为 ##岁##月
  19.       elsif MonthCount >= 12 then
  20.             Result := to_char(trunc(MonthCount/12))||'岁'||to_char(mod(MonthCount,12))||'月';

  21.       -- 如果出生1-11个月,年龄显示为 ##月##天
  22.       elsif MonthCount >= 1 then
  23.             Result := to_char(MonthCount)||'月';
  24.             if DayCount >= 1 then
  25.                   Result := Result || to_char(DayCount) || '天';
  26.             end if;

  27.       -- 如果出生1天-1个月,年龄显示为##天
  28.       elsif DayCount >= 1 then
  29.             Result := to_char(DayCount)||'天';

  30.       -- 如果出生不足1天,年龄显示为1天
  31.       else
  32.             Result := '1天';

  33.       end if;

  34.       return(Result);
  35. end;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28878983/viewspace-2133899/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-05-11

  • 博文量
    208
  • 访问量
    212338