Added out params for function as per req. p.s. Thanks to Macho for providing the patch.
This commit is contained in:
parent
06c90bf376
commit
997137287e
@ -3,34 +3,37 @@
|
|||||||
## Creating function
|
## Creating function
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE OR REPLACE FUNCTION Age_calc (p_dob IN DATE)
|
CREATE OR REPLACE FUNCTION P7(
|
||||||
RETURN VARCHAR IS
|
dob IN DATE,
|
||||||
-- declare
|
f_month OUT NUMBER,
|
||||||
ageOP VARCHAR2(255);
|
f_day OUT NUMBER
|
||||||
years NUMBER;
|
)
|
||||||
months NUMBER;
|
RETURN NUMBER
|
||||||
days NUMBER;
|
IS
|
||||||
|
current_date DATE := SYSDATE;
|
||||||
|
year_diff NUMBER;
|
||||||
|
month_diff NUMBER;
|
||||||
|
day_diff NUMBER;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
year_diff := EXTRACT(YEAR FROM current_date) - EXTRACT(YEAR FROM dob);
|
||||||
|
month_diff := EXTRACT(MONTH FROM current_date) - EXTRACT(MONTH FROM dob);
|
||||||
|
day_diff := EXTRACT(DAY FROM current_date) - EXTRACT(DAY FROM dob);
|
||||||
|
|
||||||
years := EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM p_dob);
|
IF (day_diff < 0) THEN
|
||||||
months := EXTRACT(MONTH FROM SYSDATE) - EXTRACT(MONTH FROM p_dob);
|
month_diff := month_diff - 1;
|
||||||
days := EXTRACT(DAY FROM SYSDATE) - EXTRACT(DAY FROM p_dob);
|
day_diff := day_diff + EXTRACT(DAY FROM last_day(add_months(SYSDATE, -1)));
|
||||||
|
END IF;
|
||||||
|
|
||||||
IF (days < 1) THEN
|
IF (month_diff < 0) THEN
|
||||||
months := months - 1;
|
year_diff := year_diff - 1;
|
||||||
days := days + EXTRACT(DAY FROM LAST_DAY(ADD_MONTHS(SYSDATE, -1)));
|
month_diff := month_diff + 12;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
IF (months < 1) THEN
|
f_month := month_diff;
|
||||||
years := years - 1;
|
f_day := day_diff;
|
||||||
months := months + 12;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
ageOP := 'Given person is ' || years || ' years ' || months || ' months ' || days || ' days old.';
|
RETURN year_diff;
|
||||||
|
END;
|
||||||
RETURN ageOP;
|
|
||||||
END Age_calc;
|
|
||||||
/
|
/
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -38,8 +41,13 @@ END Age_calc;
|
|||||||
## Calling function
|
## Calling function
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
|
DECLARE
|
||||||
|
years NUMBER;
|
||||||
|
months NUMBER;
|
||||||
|
days NUMBER;
|
||||||
BEGIN
|
BEGIN
|
||||||
DBMS_OUTPUT.PUT_LINE(Age_calc(TO_DATE('2004-04-28', 'YYYY-MM-DD')));
|
years := P7(TO_DATE('2003-12-02', 'YYYY-MM-DD'), months, days);
|
||||||
|
DBMS_OUTPUT.PUT_LINE('Age: ' || years || ' Years ' || months || ' Months ' || days || ' Days.');
|
||||||
END;
|
END;
|
||||||
/
|
/
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user