| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Question:Age From Birthdate
Hi,
I think your suggested SQL is only a simple and quick estimation as it is not exactly 365 days in a year. Due to the existence of leap year, some exceptions exist :
Case 1
Case 2
If I don't consider case 2, I have the following proposed SQL
select
/* get estimated age first */
to_char(sysdate,'yyyy')-to_char(birthday,'yyyy') -
/* adjust the age according to the month and day */
decode(sign(to_char(sysdate,'mm')-
to_char(birthday,'mm')),
-1,1, /* earlier month */
+1,0, /* later month */
/* same month, then determine by the day part */
0,decode(sign(to_char(sysdate,'dd')-
to_char(birthday,'dd')),
-1,1, /* earlier day */
0 /* later or same day */
))
Regards,
Kimmy.
Stanley Fung wrote:
> select trunc(trunc(sysdate-birthday)/365) from emp;
>
>
![]() |
![]() |