0% found this document useful (0 votes)
102 views11 pages

CHAPTER 7 - Function

1. This chapter discusses single-row functions, multiple-row functions, and user-defined functions in Oracle. 2. Single-row functions return one result per row based on the values in a single row. Examples include character, number, date, conversion, and general functions. 3. Multiple-row functions manipulate groups of rows and return one result per group using functions like avg, count, max, min and sum. 4. User-defined functions are stored functions that can be called by name and return a value, similar to procedures.

Uploaded by

Abdelaz IDRISSOU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views11 pages

CHAPTER 7 - Function

1. This chapter discusses single-row functions, multiple-row functions, and user-defined functions in Oracle. 2. Single-row functions return one result per row based on the values in a single row. Examples include character, number, date, conversion, and general functions. 3. Multiple-row functions manipulate groups of rows and return one result per group using functions like avg, count, max, min and sum. 4. User-defined functions are stored functions that can be called by name and return a value, similar to procedures.

Uploaded by

Abdelaz IDRISSOU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CHAPTER 7

1. Single-row function
These functions operate on single rows only and return one result per row. There are
different types of single-row functions. This lesson covers the following ones:
• Character
• Number
• Date
• Conversion
• General

Character functions: Accept character input and can return both character and
number values
• Number functions: Accept numeric input and return numeric values
• Date functions: Operate on values of the DATE data type (All date functions return
a value of DATE data type except the MONTHS_BETWEEN function, which returns
a
number.)
• Conversion functions: Convert a value from one data type to another
• General functions:
- NVL
- NVL2
- NULLIF
- COALESCE
- CASE
- DECODE
a) Character

Example:
b) Number
ROUND: Rounds value to specified decimal
TRUNC: Truncates value to specified decimal
MOD: Returns remainder of division
Example:

c) Date
The Oracle database stores dates in an internal numeric format: century, year, month,
day, hours, minutes, and seconds.
The default date display format is DD-MON-RR
SYSDATE is a function that returns: date and time
You can perform the following operations:
Example:

d) Conversion

- Implicit Data
Example:
select * from employees where hire_date > '01-JAN-1990'
select * from employees where salary = 24000
select * from employees where salary = '24000'
- Explicit Data

Example:
SELECT employee_id, TO_CHAR(hire_date, 'MM/YY') Month_Hired
FROM employees WHERE last_name = 'Higgins';
e) General
These functions work with any data type and pertain to the use of null values in the
expression list.

Example:
2. Multiple-row function
Functions can manipulate groups of rows to give one result per group of rows. These
functions are also known as group functions: Avg, count, max, min, sum

Syntax:
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];

In the syntax:
group_by_expression specifies columns whose values determine the basis for
grouping rows
group_condition restricts the groups of rows returned to those groups for which the
specified condition is true.
3. User-defined function
A stored function (also called a user function or user-defined function) is a set of
PL/SQL statements you can call by name. Stored functions are very similar to
procedures, except that a function returns a value to the environment in which it is
called. User functions can be used as part of a SQL expression
- Syntax:
CREATE [OR REPLACE] FUNCTION function_name
[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
. . .)]
RETURN datatype
IS|AS
function_body;
In syntax:
function_name Is the name of the function to be created
argument Is the name given to the function parameter (Every argument is
associated with a mode and data type. You can have any number of arguments
separated by a comma. You pass the argument when you invoke the function.)
mode Is the type of parameter (Only IN parameters should be declared.)
datatype Is the data type of the associated parameter
RETURN datatype Is the data type of the value returned by the function
function_body Is the PL/SQL block that makes up the function code
- Example
CREATE or Replace FUNCTION check_sal(p_empno employees.employee_id
%TYPE)
RETURN Boolean IS
v_dept_id employees.department_id%TYPE;
v_sal employees.salary%TYPE;
v_avg_sal employees.salary%TYPE;
BEGIN
SELECT salary,department_id INTO v_sal,v_dept_id FROM employees
WHERE employee_id=p_empno;
SELECT avg(salary) INTO v_avg_sal FROM employees
WHERE department_id=v_dept_id;
IF v_sal > v_avg_sal THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
EXCEPTION when others then return false;
end;
Invoke
BEGIN
DBMS_OUTPUT.PUT_LINE('Checking for employee with id 205');
IF (check_sal(205) IS NULL) THEN
DBMS_OUTPUT.PUT_LINE('The function returned
NULL due to exception');
ELSIF (check_sal(205)) THEN
DBMS_OUTPUT.PUT_LINE('Salary > average');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary < average');
END IF;
END;
4. Practices
a. The HR department wants to find the length of employment for each
employee. For each employee, display the last name and calculate the number
of months between today and the date on which the employee was hired. Label
the column MONTHS_WORKED. Order your results by the number of
months employed. Round the number of months up to the closest whole
number.
b. Create a report that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3 times salary>.
Label the column Dream Salaries.
c. Create a query to display the last name and salary for all employees. Format
the salary to be 15 characters long, left-padded with the $ symbol. Label the
column SALARY.
d. Display each employee’s last name, hire date, and salary review date, which is
the first Monday after six months of service. Label the column REVIEW.
Format the dates to appear in the format similar to “Monday, the Thirty-First
of July, 2000.”
e. Create a query that displays the employees’ last names and commission
amounts. If an employee does not earn commission, show “No Commission.”
Label the column COMM.
f. Find the highest, lowest, sum, and average salary of all employees. Label the
columns Maximum, Minimum, Sum, and Average, respectively. Round your
results to the nearest whole number
g. Write a query to display the number of people with the same job.
h. Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.
i. Create a query to display the total number of employees and, of that total, the
number of employees hired in 1995, 1996, 1997, and 1998
j. Create a function to display all employee of one department.

You might also like