create or replace function SF_XT_CSRQTONL_BZ ( Pcsrq00 char, --出生日期 Pcssj00 char default '00:00:00', --出生时间 Pjzrq00 char default to_char(sysdate,'yyyymmdd'), --截止日期 Pjzsj00 char default '00:00:00', --截止时间 Pfhgs00 char default '2' --返回格式:'0'标准;'1'岁月天;'2'YMD;'3'标准(1-3岁去掉月份);'4'岁月天时(用|隔开) ) -- MODIFICATION HISTORY -- Person Date Comments -- zhanghr 2019.11.15 统一年龄的计算办法 create BQHS9.0-20191115-004 -- linzetao 2020.10.20 增加返回方式4, 返回年龄 ZYSF9-20201021-003 -- jlg 2022.07.26 增加年月显示上限的处理 YJ9-20220713-002 -- linzetao 2022.10.11 增加返回方式5, 计算年龄, 保留1位小数 ZYSF9-20221011-002 -- 2025.11.01 返回方式0、3调整,0-1小时内,记录到分钟 return varchar2 is Vcsrqsj date; Vjzrqsj date; Vresult varchar2(50); Vyear00 number(10); Vmonth0 number(10); Vday000 number(10); Vday001 number(12,4); Vnyxssx XT_YYXX00.NYXSSX%type; begin if Pjzrq00 is null then Vjzrqsj:=sysdate; else Vjzrqsj:=to_date(Pjzrq00||Pjzsj00,'yyyymmddhh24:mi:ss'); end if; if Pcsrq00 is null then return '不详'; else begin Vcsrqsj:=to_date(Pcsrq00||Pcssj00,'yyyymmddhh24:mi:ss'); exception when others then return '不详'; end; end if; Vyear00:=trunc(months_between(Vjzrqsj,Vcsrqsj)/12); Vmonth0:=mod(trunc(months_between( Vjzrqsj,Vcsrqsj)),12); Vday000:=trunc(Vjzrqsj-add_months(Vcsrqsj,Vyear00*12+Vmonth0)); Vday001:=Vjzrqsj-Vcsrqsj; select nvl(max(NYXSSX),3) into Vnyxssx from XT_YYXX00 where FLAG00 = '1' and rownum = 1; if Pfhgs00 in ('0','3') then --格式0:0-72小时的,记录到小时;72小时-1周岁的,记录到 月份和天数; 1-3周岁的,记录到岁和月;3岁以上记录到岁。 -- 出生日为第0天。按按照日历计算的历法年龄,实足年龄。 --格式3:0-1小时内,记录到分钟,1-72小时的,记录到小时;72小时-1周岁的,记录到 月份和天数; 1岁以上记录到岁。 -- >3周岁 if Vyear00>=Vnyxssx then return to_char(Vyear00)||'岁'; -- 1-3周岁 elsif Vyear00>=1 and Vyear00=0 and Vday001*24<1 then return to_char(trunc(Vday001 * 24 * 60))||'分钟'; --72小时 elsif Vday001*24>=1 and Vday001*24<72 then return to_char(trunc(Vday001*24))||'小时'; --72小时-1周岁 elsif Vday001*24>=72 and Vyear00<1 then if Vmonth0>0 then return to_char(Vmonth0)||'月'||to_char(Vday000)||'天'; else return to_char(Vday000)||'天'; end if; else return '不详'; end if; elsif Pfhgs00='1' then --5岁以上,记录到岁;其余记录年月日 if Vyear00>=5 then return to_char(Vyear00)||'岁'; elsif Vday001>=0 and Vyear00<5 then return to_char(Vyear00)||'岁'||to_char(Vmonth0)||'月'||to_char(Vday000)||'天'; else return '不详'; end if; elsif Pfhgs00='2' then --5岁以上,记录到岁;其余记录年月日 if Vyear00>=5 then return to_char(Vyear00)||'Y'; elsif Vday001>=0 and Vyear00<5 then return to_char(Vyear00)||'Y'||to_char(Vmonth0)||'M'||to_char(Vday000)||'D'; else return '--'; end if; elsif Pfhgs00='4' then return to_char(Vyear00)||'|'||to_char(Vmonth0)||'|'||to_char(Vday000)||'|'||trunc((Vday001-trunc(Vday001,0))*24,0); elsif Pfhgs00='5' then return trim(to_char(round(months_between(sysdate, to_date(Pcsrq00,'YYYYMMDD'))/12,1),'990.9')); else return '不详'; end if; end; /