ОТКЛЮЧИТЬ АДБЛОК

ADBlock блокирует некоторый контент на сайте

ADBlock errore

Oracle Age calculation from Date of birth and Today

StackOverflow https://stackoverflow.com/questions/3015431

вопрос

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 months have 31 days.

I need to get the correct age with the maximum precision. How can I do that?

Решение

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>

ДРУГИЕ СОВЕТЫ

??? ??????-?????? ? ?????? ?????????? ????? (? ?????) ???????:

select months_between(TRUNC(sysdate),
                      to_date('15-Dec-2000','DD-MON-YYYY')
                     )/12
as age from dual;

       AGE
----------
9.48924731

SELECT 
TRUNC((SYSDATE - TO_DATE(DOB, 'YYYY-MM-DD'))/ 365.25) AS AGE_TODAY FROM DUAL;
??? ?????? ? ????? ? ?????.

? ???????????? ??? ????????????? ?????-???? ??????????????? ? ????? (???? ??? ?????? ??????? ? ???, ???):

SQL> with some_birthdays as
  2  ( select date '1968-06-09' d from dual union all
  3    select date '1970-06-10' from dual union all
  4    select date '1972-06-11' from dual union all
  5    select date '1974-12-11' from dual union all
  6    select date '1976-09-17' from dual
  7  )
  8  select trunc(sysdate) today
  9       , d birth_date
 10       , extract(year from numtoyminterval(months_between(trunc(sysdate),d),'month')) age
 11    from some_birthdays
 12  /

TODAY               BIRTH_DATE                 AGE
------------------- ------------------- ----------
10-06-2010 00:00:00 09-06-1968 00:00:00         42
10-06-2010 00:00:00 10-06-1970 00:00:00         40
10-06-2010 00:00:00 11-06-1972 00:00:00         37
10-06-2010 00:00:00 11-12-1974 00:00:00         35
10-06-2010 00:00:00 17-09-1976 00:00:00         33

5 rows selected.

??? ??? ?? ?????

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;

???, ???????, ??????????? ?????, ??? ???-???? ?????? ?????????

select sysdate-to_date('30-jul-1977') from dual; 

??????? (?????? ???) ????????:

SELECT
  TRUNC(months_between(sysdate, per.DATE_OF_BIRTH) / 12) AS "Age"
FROM PD_PERSONS per

SQL>select to_char(to_date('19-11-2017','dd-mm-yyyy'),'yyyy') -  to_char(to_date('10-07-1986','dd-mm-yyyy'),'yyyy') year,
to_char(to_date('19-11-2017','dd-mm-yyyy'),'mm') -  to_char(to_date('10-07-1986','dd-mm-yyyy'),'mm') month,
to_char(to_date('19-11-2017','dd-mm-yyyy'),'dd') -  to_char(to_date('10-07-1986','dd-mm-yyyy'),'dd') day from dual;

      YEAR      MONTH        DAY
---------- ---------- ----------
        31          4          9

?? ?????? ???????????

SELECT ROUND((SYSDATE - TO_DATE('12-MAY-16'))/365.25, 5) AS AGE from DUAL;
?? ?????? ????????? ROUND, ????? ????????, ??? ????? ?????? ????? ???????, ??? ?? ??????.

?????????? ???? ? ?????????? ???????, ??? ?????????????? ???????? ? ????????? ?????????? ????? ? ??.

??? ?????? ?????????? ??????. ? ???????? ????????? ?? ????? ??????????????? ?????? ? ????, ????????? TO_DATE. ??? ?? ?????, ???? ? ??? ???? ???? ???????? ? ??????? ????, ?? ?????? ??????? ??? ???? ?????????.

???????????, ??? ?? ?????? ????? ??????? (?????????? ??? ?????? ????????????? ?????), ??? ??????? ?? June 4, 1996, ?????????? ????????? ????????? ???????:

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

?????????: (??????????? 28 ??? 2019)

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

??????????:

  • SYSDATE:. ???????? (??) ??????????? ???? ???????
  • TO_DATE('04-06-1996'): ????????????? VARCHAR (??????) ? ???? ???????? DATE (???? SQL)
  • .
  • TO_NUMBER(...): ?????????????? ???? NUMBER (??? SQL)
  • ???????? ?? 365.25: ??? ????, ????? ????? ?????????? ??? ?????? ?????? ???? (4 * 0,25 = 1 ?????? ?????).
  • Trunc(...):. ???????? ??? ????? ?????? ?? ?????

?? ?????? ??????????? ???? ?????,

SELECT EXTRACT(YEAR FROM APP_SUBMITTED_DATE)-EXTRACT(YEAR FROM BIRTH_DATE) FROM SOME_TABLE; ??? ????? ???????? ??? ? ???? ??????? ??????????????.
?? ?????? ????? ???????????? SYSDATE ?????? APP_SUBMITTED_DATE.

? ?????????.

???????? (SYSDATE-DOB) / 365 "???????" ?? ????????

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow