SQL 3
SQL 3
Objectives
Input Output
Function
arg 1 Function
performs action
arg 2
Result
value
arg n
Two Types of SQL Functions
Functions
Single-row Multiple-row
functions functions
Single-Row Functions
–– Manipulate
Manipulate data
data items
items
–– Accept
Accept arguments
arguments andand return
return one
one value
value
–– Act
Act on
on each
each row
row returned
returned
–– Return
Return one
one result
result per
per row
row
–– May
May modify
modify the
the datatype
datatype
–– Can
Can be
be nested
nested
function_name
function_name (column|expression,
(column|expression, [arg1,
[arg1, arg2,...])
arg2,...])
Single-Row Functions
Character
General Number
Single-row
functions
Conversion Date
Character Functions
Character
functions
EMPNO
EMPNO ENAME
ENAME DEPTNO
DEPTNO
---------
--------- ----------
---------- ---------
---------
7698
7698 BLAKE
BLAKE 30
30
Character Manipulation
Functions
• Manipulate character strings
Function Result
CONCAT('Good', 'String') GoodString
SUBSTR('String',1,3) Str
LENGTH('String') 6
INSTR('String', 'r') 3
LPAD(sal,10,'*') ******5000
Using the Character
Manipulation Functions
SQL> SELECT ename, CONCAT (ename, job), LENGTH(ename),
2 INSTR(ename, 'A')
3 FROM emp
4 WHERE SUBSTR(job,1,5) = 'SALES';
–– Add
Add or or subtract
subtract aa number
number to
to or
or from
from aa date
date
for
for aa resultant
resultant date
date value.
value.
–– Subtract
Subtract two
two dates
dates to
to find
find the
the number
number of
of
days
days between
between those
those dates.
dates.
–– Add
Add hours
hours to
to aa date
date by
by dividing
dividing the
the number
number
of
of hours
hours by
by 24.
24.
Using Arithmetic Operators
with Dates
SQL> SELECT ename, (SYSDATE-hiredate)/7 WEEKS
2 FROM emp
3 WHERE deptno = 10;
ENAME WEEKS
---------- ---------
KING 830.93709
CLARK 853.93709
MILLER 821.36566
Date Functions
Function Description
• LAST_DAY('01-SEP-95') '30-SEP-95'
Using Date Functions
• ROUND('25-JUL-95','MONTH') 01-AUG-95
• ROUND('25-JUL-95','YEAR') 01-JAN-96
• TRUNC('25-JUL-95','MONTH') 01-JUL-95
• TRUNC('25-JUL-95','YEAR') 01-JAN-
95
Datatype Conversion Function
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
TO_CHAR Function with Dates
TO_CHAR(date,
TO_CHAR(date, 'fmt')
'fmt')
ENAME HIREDATE
---------- -----------------
KING 17 November 1981
BLAKE 1 May 1981
CLARK 9 June 1981
JONES 2 April 1981
MARTIN 28 September 1981
ALLEN 20 February 1981
...
14 rows selected.
TO_CHAR Function with Numbers
TO_CHAR(number,
TO_CHAR(number, 'fmt')
'fmt')
• Use these formats with the TO_CHAR
function to display a number value as a
character:
9 Represents a number
0 Forces a zero to be displayed
$ Places a floating dollar sign
L Uses the floating local currency symbol
. Prints a decimal point
, Prints a thousand indicator
Using TO_CHAR Function
with Numbers
SQL> SELECT TO_CHAR(sal,'$99,999') SALARY
2 FROM emp
3 WHERE ename = 'SCOTT';
SALARY
--------
$3,000
TO_NUMBER and TO_DATE
Functions
–– Convert
Convert aa character
character string
string to
to aa number
number
format
format using
using the
the TO_NUMBER
TO_NUMBER functionfunction
TO_NUMBER(char[,
TO_NUMBER(char[, 'fmt'])
'fmt'])
•• Convert
Convert aa character
character string
string to
to aa date
date
format
format using
using the
the TO_DATE
TO_DATE function
function
TO_DATE(char[,
TO_DATE(char[, 'fmt'])
'fmt'])
NVL Function
• Converts null to an actual value
–– Datatypes
Datatypes that
that can
can be
be used
used are
are date,
date,
character,
character, and
and number.
number.
–– Datatypes
Datatypes must
must match
match
•• NVL(comm,0)
NVL(comm,0)
•• NVL(hiredate,'01-JAN-97')
NVL(hiredate,'01-JAN-97')
•• NVL(job,'No
NVL(job,'No Job
Job Yet')
Yet')
Using the NVL Function
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
Nesting Functions
ENAME NVL(TO_CHAR(MGR),'NOMANAGER')
---------- -----------------------------
KING No Manager
Summary