Press "Enter" to skip to content

Oracle Function Example (Return Number)

In this article, I am giving some Oracle Function examples returning number values.

Oracle Function Example (Return Number)

The following Oracle function example will return the percentage (number) of the first parameter by calculating it with the second parameter.

CREATE OR REPLACE FUNCTION calc_percentage (p_1 IN NUMBER, p_2 IN NUMBER)
RETURN NUMBER
IS
n_pct NUMBER := 0;
BEGIN
IF p_1 IS NOT NULL AND p_2 IS NOT NULL
THEN
n_pct := (p_1 * p_2) / 100;
END IF;

RETURN n_pct;
END calc_percentage;
/

Test:

SELECT calc_percentage (40, 5) percentage FROM DUAL;

Output:

PERCENTAGE
----------
2
1 row selected.

In the following example, the function will return the number by converting the current date to a number without any parameter.

CREATE OR REPLACE FUNCTION date_in_number
RETURN NUMBER
IS
BEGIN
RETURN (TO_NUMBER (TO_CHAR (SYSDATE, 'yyyymmdd')));
END date_in_number;
/

Test:

SELECT date_in_number FROM DUAL;

Output:

DATE_IN_NUMBER
--------------
20180807
1 row selected.

See also: