如何使用Oracle获得年龄,月份和天数的年龄 [英] How to get age in years,months and days using Oracle

查看:179
本文介绍了如何使用Oracle获得年龄,月份和天数的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下格式为每个人打印其年龄:

I'm trying to print for each person its age using this format :

例如:19年8个月13天.

E.g : 19 years , 8 months , 13 days.

我在Google上搜索了很多,并且注意到有一个特定的函数可以计算日期DATEDIFF之间的差.

I've googled a lot and I've noticed that there is a specific function to calculate the difference between dates DATEDIFF.

但是SQL*Plus中不存在此功能,所以我继续尝试使用MONTHS_BETWEEN()和一些运算符.

However this function does not exist in SQL*Plus , so I went on trying using MONTHS_BETWEEN() and some operators.

我的尝试:

SELECT name , ' ' || 
    FLOOR(MONTHS_BETWEEN(to_date(SYSDATE),to_date(date_of_birth))/12)||' years ' ||  
    FLOOR(MOD(MONTHS_BETWEEN(to_date(SYSDATE),to_date(date_of_birth)),12)) || ' months ' || 
    FLOOR(MOD(MOD(MONTHS_BETWEEN(to_date(SYSDATE),to_date(date_of_birth)),12),4))|| ' days ' AS "Age"
FROM persons;

我的问题取决于日子.我不知道如何使用此函数计算天数(尝试除以4或30");我以为我的逻辑不好,但是我无法弄清楚,有什么想法吗?

My issue relies on getting the days. I don't know how should I calculate the days , using this function ('tried dividing by 4 , or 30); I'm thinking my logic is bad but I can't figure it out , any ideas ?

推荐答案

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

Very similar to Lalit's answer, but you can get an accurate number of days without assuming 30 days per month, by using add_months to adjust by the total whole-month difference:

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

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

You can verify by reversing the calculation:

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

这篇关于如何使用Oracle获得年龄,月份和天数的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆