oracle年龄计算函数天 月 年,如何使用Oracle获得年龄,月份和天数的年龄

小编典典

与Lalit的答案非常相似,但是您可以add_months通过按整个月的总差异进行调整来获得准确的天数,而无需假设每月有30天:

select sysdate,

hiredate,

trunc(months_between(sysdate,hiredate) / 12) as years,

trunc(months_between(sysdate,hiredate) -

(trunc(months_between(sysdate,hiredate) / 12) * 12)) as months,

trunc(sysdate)

- add_months(hiredate, trunc(months_between(sysdate,hiredate))) as days

from emp;

SYSDATE HIREDATE YEARS MONTHS DAYS

---------- ---------- ---------- ---------- ----------

2015-10-26 1980-12-17 34 10 9

2015-10-26 1981-02-20 34 8 6

2015-10-26 1981-02-22 34 8 4

2015-10-26 1981-04-02 34 6 24

2015-10-26 1981-09-28 34 0 28

2015-10-26 1981-05-01 34 5 25

2015-10-26 1981-06-09 34 4 17

2015-10-26 1982-12-09 32 10 17

2015-10-26 1981-11-17 33 11 9

2015-10-26 1981-09-08 34 1 18

2015-10-26 1983-01-12 32 9 14

2015-10-26 1981-12-03 33 10 23

2015-10-26 1981-12-03 33 10 23

2015-10-26 1982-01-23 33 9 3

您可以通过反转计算来验证:

with tmp as (

select trunc(sysdate) as today,

hiredate,

trunc(months_between(sysdate,hiredate) / 12) as years,

trunc(months_between(sysdate,hiredate) -

(trunc(months_between(sysdate,hiredate) / 12) * 12)) as months,

trunc(sysdate)

- add_months(hiredate, trunc(months_between(sysdate,hiredate))) as days

from emp

)

select * from tmp

where today != add_months(hiredate, (12 * years) + months) + days;

no rows selected

2021-03-10

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值