Sunday, 15 August 2010

oracle - PL/SQL Program to calculate age and whether your birthday year is a leap year or not -



oracle - PL/SQL Program to calculate age and whether your birthday year is a leap year or not -

so question seems asked in various forms in programming classes. i've seen asked triggers, functions, , such. i've calculated jump year part of program, , there couple of different ways calculate age birthday, whether dividing 365.25 or intervals months between , couple of other ways go best way.

i'm having problems age calculation , getting info overload on doing in other programs , programming in pl/sql oracle dev.

this 1 shows how it's done in sql server: calculating age sysdate , birthdate using sql server

this 1 uses triggers , similar not quite i'm looking for. calculating age birthday oracle plsql trigger , insert age in table

write pl/sql block take birthdate. calculates , prints age in years including 1 decimal point. checks whether birth year jump year or not. print message “my birth year jump year.” or “my birth year not jump year.” hint: determine jump year, year should divisible 4 not divisible 100, or should divisible 400.

set serveroutput on declare v_birthday_year number(4) := &v_birthday_year; v_your_age number(4, 1); v_leap_remainder1 number(5, 2); v_leap_remainder2 number(5, 2); v_leap_remainder3 number(5, 2); begin v_leap_remainder1 := mod(v_birthday_year, 4); v_leap_remainder2 := mod(v_birthday_year, 100); v_leap_remainder3 := mod(v_birthday_year, 400); if ((v_leap_remainder1 = 0 , v_leap_remainder2 <> 0 ) or v_leap_remainder3 = 0) dbms_output.put_line(v_birthday_year || ' jump year'); else dbms_output.put_line (v_birthday_year || ' not jump year'); end if; to_char(sysdate, 'dd-mm-yyyy') v_your_age := (months_between(trunc(sysdate), v_birthday_year)/12); dbms_output.put_line ('your age ' || v_your_age); end; /

actually figured out, to_date conversion dob, separate variables birthyear, , dob, , age. age calculated 1 decimal. trunc(x, 1) works great now.

you can seek check conversions julian dates. when convert timestamp julian date number of days since date before -4000 bc. if calculate difference between 2 dates know number of days between dates. jan 1st , dec 31st of birth year , you'll know if jump year or not.

for calculating age check function months_between (check lastly reply this question).

oracle plsql

No comments:

Post a Comment