PL/SQL Example: Calculate Your Age
Published on 10-AUG-2016 by USER5
The following will beshown examples of PL/SQL to calculate a person's age. Person's age can be determined from the difference between the current date with the date of birth.
--ENter your birth data with DD-MM-YYYY (ex: 17-02-1990)
tlahir := TO_DATE('&indate', 'DD-MM-YYYY');
umur := FLOOR((SYSDATE - tlahir)/365);
DBMS_OUTPUT.PUT_LINE ('Your age is '||umur||' years old');
Above PL/SQL block have 2 variables, tlahir with DATE data type and age with NUMBER data type.Tlahir variable will receive input from a keyboard entry date of birth in the format DD-MM-YYYY.Umur variable will make the process of calculating the difference between SYSDATE (current date)with tlahir (date of birth) were divided by 365 (days in a year). To generate integers by rounding downis used FLOOR function. If the input is "17-02-1990" then the output is "Your age is 23 years old".