Oracle PL/SQL, How to calculate age from date birth using simple substitution variable and months_between

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

I am working on a simple PL/SQL block that asks the user their Date Of Birth and calculates their age. I've used the months_between divided by 12 conversion but I am having trouble with the date conversion and date input from the user for the substitution variable. Please ignore the v_birthday_year as that is another part of the code I have to integrate in later.

I'm not sure the format that the user has to type in, ie 05-07-1980, or 06 Mar 1978 that would be acceptable, and how to calculate their age from what they put in.

My desired program is to write a PL/SQL program to accept the user's birthdate in this format DD-MON-YYYY, and calculates the Age in with 1 Decimal place. So I need it calculated out to one decimal place too.

Here is what I have, but I'm not sure what format to type in the DOB at the substitution variable input and the actual calculation with one decimal place. I'm just trying to see where I'm going wrong and how to correct it.

SET SERVEROUTPUT ON DECLARE   --v_birthday_year         NUMBER(4)    := &v_birthday_year;   v_dob                   DATE      := &v_dob    v_your_age              NUMBER(3, 1); BEGIN   v_your_age := TRUNC(MONTHS_BETWEEN(SYSDATE, v_dob))/12;     DBMS_OUTPUT.PUT_LINE ('Your age is ' || v_your_age); END; /

回答1:

All SQL examples from others and me are probably better thing to use unless you are learning something about PL/SQL. Here's what I came up with:

 DECLARE   v_dob        DATE:= to_date('&v_dob', 'MM/DD/YYYY'); -- copy this line and you'll be fine   v_your_age   NUMBER(3, 1); BEGIN   v_your_age := TRUNC(MONTHS_BETWEEN(SYSDATE, v_dob))/12;     DBMS_OUTPUT.PUT_LINE ('Your age is ' || v_your_age); END; /  I passed 01/01/2010. Got this in output:    Your age is 3.1

Another SQL example:

SELECT empno, ename, hiredate     , TRUNC(MONTHS_BETWEEN(sysdate, hiredate)/12) years_of_service    FROM scott.emp /


回答2:

You may use any of these (pure sql)

SELECT months_between(sysdate, user_birth_date) /12 FROM dual;

or

SELECT floor(months_between(sysdate, user_birth_date) /12) FROM  dual;

or

SELECT EXTRACT(YEAR FROM sysdate) - EXTRACT(YEAR FROM user_birth_date) FROM dual

or

SELECT (sysdate - user_birth_date) / 365.242199 FROM dual

Hope this will help



回答3:

 Create or replace function getAge(birthday date) return number is   v_your_age   NUMBER(3, 1);``   BEGIN   v_your_age := floor(MONTHS_BETWEEN(SYSDATE, birthday))/12;     return v_your_age;   END;    /

then you call the function

BEGIN DBMS_OUTPUT.PUT_LINE ('Your age is ' || getAge(to_date('10/10/2015','DD/MM/YYYY'))); end; /


回答4:

select dob,sysdate,trunc(months_between(sysdate,dob)/12) as year,    trunc(mod(months_between(sysdate,dob),12)) as month,    trunc(sysdate - add_months(dob,((trunc(months_between(sysdate,dob)/12)*12) + trunc(mod(months_between(sysdate,dob),12))))) as day    from emp;


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!