Professional Documents
Culture Documents
Single-Row Functions
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe various
various types
types of
of functions
functions
available
available in
in SQL
SQL
•• Use
Use character,
character, number,
number, andand date
date
functions
functions in
in SELECT
SELECT statements
statements
•• Describe
Describe the
the use
use of
of conversion
conversion
functions
functions
Input Output
Function
arg 1 Function
performs action
arg 2
Result
value
arg n
Functions
Single-row Multiple-row
functions functions
Character
General Number
Single-row
functions
Conversion Date
Convert
Convert case
case for
for character
character strings
strings
Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
EMPNO
EMPNO ENAME
ENAME DEPTNO
DEPTNO
---------
--------- ----------
---------- ---------
---------
7698
7698 BLAKE
BLAKE 30
30
Manipulate
Manipulate character
character strings
strings
Function Result
CONCAT('Good', 'String') GoodString
SUBSTR('String',1,3) Str
LENGTH('String') 6
INSTR('String', 'r') 3
LPAD(sal,10,'*') ******5000
•• MOD:
MOD: Returns
Returns remainder
remainder of
of division
division
MOD(1600,
MOD(1600, 300)
300) 100
100
3-12 Copyright Oracle Corporation, 1998. All rights reserved.
Using the ROUND Function
•• 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 toto aa date
date by
by dividing
dividing the
the
number
number of of hours
hours byby 24.
24.
ENAME WEEKS
---------- ---------
KING 830.93709
CLARK 853.93709
MILLER 821.36566
• LAST_DAY('01-SEP-95') '30-SEP-95'
• 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
For
For assignments,
assignments, the
the Oracle
Oracle can
can
automatically
automatically convert
convert the
the following:
following:
From To
VARCHAR2 or CHAR NUMBER
NUMBER VARCHAR2
DATE VARCHAR2
For
For expression
expression evaluation,
evaluation, the
the Oracle
Oracle Server
Server
can
can automatically
automatically convert
convert the
the following:
following:
From To
VARCHAR2 or CHAR NUMBER
TO_CHAR TO_CHAR
TO_CHAR(date,
TO_CHAR(date, 'fmt')
'fmt')
The
The format
format model:
model:
•• Must
Must bebe enclosed
enclosed in
in single
single quotation
quotation marks
marks
and
and is
is case
case sensitive
sensitive
•• Can
Can include
include any
any valid
valid date
date format
format element
element
•• Has
Has an
an fm
fm element
element to
to remove
remove padded
padded
blanks
blanks oror suppress
suppress leading
leading zeros
zeros
•• Is
Is separated
separated from
from the
the date
date value
value by
by aa
comma
comma
3-27 Copyright Oracle Corporation, 1998. All rights reserved.
Elements of Date Format Model
• Time
Time elements
elements format
format the
the time
time portion
portion of
of
the
the date.
date.
HH24:MI:SS AM 15:45:32 PM
• Add
Add character
character strings
strings by
by enclosing
enclosing them
them
in
in double
double quotation
quotation marks.
marks.
DD "of" MONTH 12 of OCTOBER
• Number
Number suffixes
suffixes spell
spell out
out numbers.
numbers.
ddspth fourteenth
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.
Use
Use these
these formats
formats with
with the
the TO_CHAR
TO_CHAR
function
function to
to display
display aa number
number value
value as
as aa
character:
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
3-31 Copyright Oracle Corporation, 1998. All rights reserved.
Using TO_CHAR Function
with Numbers
SALARY
--------
$3,000
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'])
0–49 50–99
If two digits The return date is in The return date is in
of the 0–49 the current century the century before
current the current one
year are: The return date is The return date is in
50–99 in the century after the current century
the current one
Converts
Converts null
null to
to an
an actual
actual value
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')
Facilitates
Facilitates conditional
conditional inquiries
inquiries by
by doing
doing
the
the work
work of
of aa CASE
CASE or
or IF-THEN-ELSE
IF-THEN-ELSE
statement
statement
DECODE(col/expression,
DECODE(col/expression, search1,
search1, result1
result1
[,
[, search2,
search2, result2,...,]
result2,...,]
[,
[, default])
default])
•• Single-row
Single-row functions
functions can
can be
be nested
nested to
to
any
any level.
level.
•• Nested
Nested functions
functions are
are evaluated
evaluated from
from
deepest
deepest level
level to
to the
the least-deep
least-deep level.
level.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
ENAME NVL(TO_CHAR(MGR),'NOMANAGER')
---------- -----------------------------
KING No Manager
Use
Use functions
functions to
to do
do the
the following:
following:
•• Perform
Perform calculations
calculations on
on data
data
•• Modify
Modify individual
individual data
data items
items
•• Manipulate
Manipulate output
output for
for groups
groups of
of rows
rows
•• Alter
Alter date
date formats
formats for
for display
display
•• Convert
Convert column
column datatypes
datatypes
•• Creating
Creating queries
queries that
that require
require the
the use
use of
of
numeric,
numeric, character,
character, and
and date
date functions
functions
•• Using
Using concatenation
concatenation with
with functions
functions
•• Writing
Writing case-insensitive
case-insensitive queries
queries toto test
test
the
the usefulness
usefulness of
of character
character functions
functions
•• Performing
Performing calculations
calculations ofof years
years and
and
months
months of
of service
service for
for an
an employee
employee
•• Determining
Determining the
the review
review date
date for
for an
an
employee
employee
3-43 Copyright Oracle Corporation, 1998. All rights reserved.