How old are you?

I just come back from holiday, I am quite busy at the moment. Here is a tiny function to get the age

trunc((to_char(sysdate,’YYYYMMDD’)-to_char(birthdate,’YYYYMMDD’))/10000)

it is much safer than add_months, because add_months do some conversion at the end of the month, and I would never accept to wait until Feb 29th, 2008 (28-2-1990 + 18*12 months) to be 18 if I were born Feb 28th, 1990.

Put your code in <code> and </code> tags

2 Responses to “How old are you?”

  1. Anonymous Says:

    oh greate!!

    def birthdate = to_date(’2000/02/29′,’yyyy/mm/dd’)
    def sys_date = to_date(’2003/02/28′,’yyyy/mm/dd’)

    select
    trunc((to_char(&sys_date,’YYYYMMDD’)-to_char(&birthdate,’YYYYMMDD’))/10000) as age from dual;

    def sys_date = to_date(’2003/03/01′,’yyyy/mm/dd’)

    select
    trunc((to_char(&sys_date,’YYYYMMDD’)-to_char(&birthdate,’YYYYMMDD’))/10000) as age from dual;

  2. Laurent Schneider » Blog Archive » Are you really 18 years old today? Says:

    […] or check an old post of mine How old are you […]

Leave a Reply

Use <code> and </code> to post code