中文年龄函数


  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博客 ” ,链接:http://blog.itpub.net/28878983/viewspace-2133899/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28878983/viewspace-2133899/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值