- create or replace function CHINESEAGE(BirthDate in date, NowDate in date) return string is
- Result string(80); -- 返回值
- MonthCount number(4,0); -- 出生的月份
- DayCount number(6,0); -- 去除月份后的余数(天)
- begin
- -- 如果生日大于当前日期,则提示错误
- if BirthDate > NowDate or BirthDate is null or NowDate is null Then
- Result := '不详';
- return(Result);
- end if;
-
- -- 获取出生多少个月余多少天
- SELECT trunc(Mon), trunc((Mon - trunc(Mon)) * 31)
- INTO MonthCount,DayCount
- FROM (SELECT months_between(NowDate, BirthDate) Mon FROM dual) V;
-
- -- 如果出生60个月以上,年龄显示为 ##岁
- if MonthCount >= 60 then
- Result := to_char(trunc(MonthCount/12))||'岁';
-
- -- 如果出生12-59个月,年龄显示为 ##岁##月
- elsif MonthCount >= 12 then
- Result := to_char(trunc(MonthCount/12))||'岁'||to_char(mod(MonthCount,12))||'月';
-
- -- 如果出生1-11个月,年龄显示为 ##月##天
- elsif MonthCount >= 1 then
- Result := to_char(MonthCount)||'月';
- if DayCount >= 1 then
- Result := Result || to_char(DayCount) || '天';
- end if;
-
- -- 如果出生1天-1个月,年龄显示为##天
- elsif DayCount >= 1 then
- Result := to_char(DayCount)||'天';
-
- -- 如果出生不足1天,年龄显示为1天
- else
- Result := '1天';
-
- end if;
-
- return(Result);
- end;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28878983/viewspace-2133899/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28878983/viewspace-2133899/