Oracle Age calculation from Date of birth and Today

前端 未结 13 1408
Happy的楠姐
Happy的楠姐 2020-11-27 15:31

I want to calculate the current Age from Date of Birth in my Oracle function.

What I am using is (Today-Dob)/30/12, but this is not accurate as some mon

相关标签:
13条回答
  • 2020-11-27 15:47

    You can try below method,

    SELECT EXTRACT(YEAR FROM APP_SUBMITTED_DATE)-EXTRACT(YEAR FROM BIRTH_DATE) FROM SOME_TABLE;

    It will compare years and give age accordingly.
    You can also use SYSDATE instead of APP_SUBMITTED_DATE.

    Regards.

    0 讨论(0)
  • 2020-11-27 15:51

    For business logic I usually find a decimal number (in years) is useful:

    select months_between(TRUNC(sysdate),
                          to_date('15-Dec-2000','DD-MON-YYYY')
                         )/12
    as age from dual;
    
           AGE
    ----------
    9.48924731
    
    0 讨论(0)
  • 2020-11-27 15:52
    SQL> select trunc(months_between(sysdate,dob)/12) year,
      2         trunc(mod(months_between(sysdate,dob),12)) month,
      3         trunc(sysdate-add_months(dob,trunc(months_between(sysdate,dob)/12)*12+trunc(mod(months_between(sysdate,dob),12)))) day
      4  from (Select to_date('15122000','DDMMYYYY') dob from dual);
    
          YEAR      MONTH        DAY
    ---------- ---------- ----------
             9          5         26
    
    SQL>
    
    0 讨论(0)
  • 2020-11-27 15:53

    Suppose that you want to have the age (number of years only, a fixed number) of someone born on June 4, 1996, execute this command :

    SELECT TRUNC(TO_NUMBER(SYSDATE - TO_DATE('04-06-1996')) / 365.25) AS AGE FROM DUAL;
    

    Result : (Executed May 28, 2019)

           AGE
    ----------
            22
    

    Explanation :

    • SYSDATE : Get system's (OS) actual date.
    • TO_DATE('04-06-1996') : Convert VARCHAR (string) birthdate into DATE (SQL type).
    • TO_NUMBER(...) : Convert a date to NUMBER (SQL type)
    • Devide per 365.25 : To have a bissextile year every four years (4 * 0.25 = 1 more day).
    • Trunc(...) : Retrieve the entire part only from a number.
    0 讨论(0)
  • 2020-11-27 15:55
    SELECT 
    TRUNC((SYSDATE - TO_DATE(DOB, 'YYYY-MM-DD'))/ 365.25) AS AGE_TODAY FROM DUAL;
    

    This is easy and straight to the point.

    0 讨论(0)
  • 2020-11-27 15:56

    Or how about this?

    with some_birthdays as
    ( 
        select date '1968-06-09' d from dual union all
        select date '1970-06-10' from dual union all
        select date '1972-06-11' from dual union all
        select date '1974-12-11' from dual union all
        select date '1976-09-17' from dual
    )
    select trunc(sysdate) today
    , d birth_date
    , floor(months_between(trunc(sysdate),d)/12) age
    from some_birthdays;
    
    0 讨论(0)
提交回复
热议问题