Connect with Facebook

Monday, January 05, 2009

What is their age?

The script below is a function for counting age:

CREATE OR REPLACE FUNCTION SPEAKER.F_Age (Dob_in IN DATE)
RETURN INTERVAL YEAR TO MONTH IS
--=======================================
-- Function to counting age
-- Version 2009 01 05
--=======================================
Retval INTERVAL YEAR TO MONTH;
BEGIN
RETURN (Sysdate - Dob_in) YEAR TO MONTH;
END F_Age;

where the input parameter is date of birth
Then in SQL*Plus try it:

sql>select empname, hiredate as Dob,F_Age(hiredate) Age from scott.emp;

ENAME |DOB |AGE
----------|---------|---------------
SMITH |17-DES-80|+000000028-01
ALLEN |20-PEB-81|+000000027-11
WARD |22-PEB-81|+000000027-10
JONES |02-APR-81|+000000027-09
MARTIN |28-SEP-81|+000000027-03
BLAKE |01-MEI-81|+000000027-08
CLARK |09-JUN-81|+000000027-07
SCOTT |19-APR-87|+000000021-09
KING |17-NOP-81|+000000027-02
TURNER |08-SEP-81|+000000027-04
ADAMS |23-MEI-87|+000000021-07
JAMES |03-DES-81|+000000027-01
FORD |03-DES-81|+000000027-01
MILLER |23-JAN-82|+000000026-11

14 rows selected.

sql>
It showed the age in year and month.

0 comments:

Post a Comment