从出生日期和今天计算 Oracle 年龄

2021-12-06 00:00:00 datetime oracle

我想在我的 Oracle 函数中从出生日期计算当前年龄.

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

我使用的是 (Today-Dob)/30/12,但这并不准确,因为有些月份有 31 天.

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>

相关文章