出生年月日是患者实体中的一个属性.而不是年龄.当然,在一些检查/检验实体中,年龄确是一个有意义的属性.年龄计算可以直接用一个SQL语句,由患者实体的出生日期计算得到,也可以用其它的方式得到.这里的SQL语句是用一个PLSQL的函数完成的.从语句上看,实践证明能够得到医院的认可.
CREATE OR REPLACE FUNCTION "F_AGE" (
Birthday in date ,
Date_Contrast in date := sysdate
)
Return varchar2
as
Age varchar2(50) ;
Temp_Month varchar2(50);
Begin
select trunc(months_between(Date_Contrast,Birthday),0) into Temp_Month from dual;
If Temp_Month >=72 then
select trunc(Temp_Month/12,0) || '岁' into Age from dual;
Return Age;
End if;
If Temp_Month >=12 then
select trunc(Temp_Month/12,0) || '岁' || to_char(Temp_Month-trunc(Temp_Month/12,0)*12) || '月' into Age from dual;
Return Age;
End if;
If Temp_Month >=1 then
select Temp_Month || '月' || decode(trunc(Date_Contrast-add_months(Birthday,Temp_Month),0),0,1,trunc(Date_Contrast-add_months(Birthday,Temp_Month),0)) || '天' into Age from dual;
Return Age;
End if;
If Temp_Month =0 then
select decode(trunc(Date_Contrast-add_months(Birthday,Temp_Month),0),0,1,trunc(Date_Contrast-add_months(Birthday,Temp_Month),0)) || '天' into Age from dual;
Return Age;
End if;
End; |