SQL Function
SQL Function
What Is a Function?
A function is a programming unit returning a single value, allowing values to
be passed in as parameters. The parameters can change the outcome or return
the result of a function. The beauty of a function is that it is self-contained
and can thus be embedded in an expression.
By definition, in Oracle SQL an expression is a SQL code command or even
another function.
SQL Functions
Functions are very powerful feature of SQL and can be used to do the
following:
Note: Most of the functions described in this lesson are specific to Oracle's
versions.
1
Types of Functions
In general, functions in Oracle SQL are divided into five groups, the first
group being the topic of this chapter, namely, single row functions. Other
function types are aggregate functions, which create groups of rows; analytic
functions, which also group but allow in-depth data analysis; object reference
functions, allowing access to special object pointers; and finally user-defined
functions, such as those you create using a programming language such as
PL/SQL.
• Single Row Functions— Single row functions can be used to execute
an operation on each row of a query. In other words, a single row
function can be used to execute the same operation for every row a
query retrieves.
• Aggregate Functions— These functions summarize repeating groups
in a row set into distinct groups, aggregating repetitive values into
items such as sums or averages.
• Analytic Functions— Unlike aggregates, which summarize repetitions
into unique items, analytics create subset summaries within aggregates.
• Object Reference Functions— These functions use pointers to
reference values. Commonly, object reference functions either
reference objects or dereference values from objects.
• User-Defined Functions— Custom functions can be built using
PL/SQL, allowing extension of the large library of Oracle SQL built-in
functionality. PL/SQL is beyond the scope of this book..
2
Single Row Functions
Now let's focus on the subject matter of this chapter. Recall that a single row
function was defined as a function that can be used to execute an operation on
each row of a query.
Let's start this journey by discovering the different classifications for single
row functions:
3
User-Defined Functions
A user-defined function is a function created by a user in addition to the
available Oracle built-in functions will not suffice. Creating your own
4
CHARACTER FUNCTIONS
5
Examples
SELECT ASCII('Ç')
FROM DUAL;
ASCII ( 'Ç' )
199
6
SELECT INSTR('MISSISSIPPI' , 'S' ,5 , 2)
FROM DUAL;
INSTR('MISSISSIPPI','S',5,2)
7
RPAD(ENAME,20,'*')
SMITH***************
ALLEN***************
14 rows selected.
SELECT ENAME
7
FROM EMP
WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE');
ENAME
SMITH
Character functions:
A. Case Conversion Functions
• LOWER
• UPPER
• INITCAP
• SUBSTR
• LENGTH
• INSTR
• LPAD
• RPAD
• TRIM
• LTRIM
• RTRIM
• ASCII
8
• CHR
• REPLACE
• TRANSLATE
9
Case Conversion Functions
Function Result
LOWER('SQL Course') sql course
10
Using Case Conversion Functions
Display the employee number, name, and department number for
employee Blake.
SELECT empno, ename, deptno
FROM emp
WHERE ename = 'blake';
no rows selected
The WHERE clause of the first SQL statement specifies the employee name
as ' blake.' Since all the data in the EMP tabls is stored in uppercase. the
name ' blake' does not ffind a match in the EMP table and as a result no rows
are selected.
..........................................................
The WHERE clause of the second SQL statement specifies that the employee
name in the EMP table be converted to lowercase and then be compared to
'blake ' . Since both the names are in lowercase now, a match is found and
one row is selected. The WHERE clause can be rewritten in the following
manner to produce the same result:
11
Using Character Manipulation Functions
Note: RPAD character manipulation function pads the character value lelt-
justified.
Using the Character Manipulation
Functions (continued)
Example:
The slide example displays ernployee name and job joined together,
length of the employee name, and the numeric position of the letter A in
the employee name, forall employees who are in sales.