
- •Objectivesj i
- •SQL Functionsi
- •Two Types of SQL Functionsi
- •Singlei le-Row Functionsi
- •Singlei le-Row Functionsi
- •Character Functionsi
- •Case Conversioni Functionsi
- •Usingi Case Conversioni Functionsi
- •Character Manipulationi l i Functionsi
- •Usingi the Character
- •Number Functionsi
- •Usingi the ROUND Functioni
- •Usingi the TRUNC Functioni
- •Usingi the MOD Functioni
- •Workingi withi Dates
- •Arithmetici ic withi Dates
- •Usingi Arithmetici ic Operators withi Dates
- •Date Functionsi
- •Usingi Date Functionsi
- •Usingi Date Functionsi
- •Conversioni Functionsi
- •ImplicitI li it Datatype Conversioni
- •ImplicitI li it Datatype Conversioni
- •Explicitli it Datatype Conversioni
- •TO_CHAR Functioni withi Dates
- •Date Format Modell Elementsl
- •Date Format Modell Elementsl
- •Usingi TO_CHAR Functioni
- •TO_CHAR Functioni withi Numbers
- •Usingi TO_CHAR Functioni
- •TO_NUMBER and TO_DATE Functionsi
- •RR Date Format
- •NVL Functioni
- •Usingi the NVL Functioni
- •DECODE Functioni
- •Usingi the DECODE Functioni
- •Nestingi Functionsi
- •Nestingi Functionsi
- •Summary
- •Practicei Overviewi

TO_NUMBER and TO_DATE Functionsi
•• Convertrt a characterr t r stringtri toto a numberr formatf r t usingi thet TO_NUMBER functionf ti
TO_NUMBER(char)
•• Convertrt a characterr t r stringtri toto a datete formatf r t usingi thet TO_DATE functionf ti
TO_DATE(char[, 'fmt'])
3-32

RR Date Format
Current Year |
Specified Date |
RR Format |
|
YY Format |
1995 |
27-OCT-95 |
1995 |
|
1995 |
1995 |
27-OCT-17 |
2017 |
|
1917 |
2001 |
27-OCT-17 |
2017 |
|
2017 |
2001 |
27-OCT-95 |
1995 |
|
2095 |
If the specified two-digit year is
|
|
0-49 |
50-99 |
If two digits |
0-49 |
The return date is in |
The return date is in |
of the |
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. |
|
3-33

NVL Functioni
Converts null to an actual value
•• Datatypest t thatt t can be used arere date,t , character,r t r, and numberr..
•• Datatypest t mustt matcht
–NVL(comm,0)( , )
–NVL(hiredate,'01( ir t ,' --JAN--97')')
–NVL(job,'No(j ,' Job Yet')t')
3-34

Usingi the NVL Functioni
SQL> SELECT ename, sal, comm, (sal*12)+NVL(comm,0) 2 FROM emp;
ENAME |
SAL |
COMM (SAL*12)+NVL(COMM,0) |
|
---------- |
--------- --------- -------------------- |
||
KING |
5000 |
|
60000 |
BLAKE |
2850 |
|
34200 |
CLARK |
2450 |
|
29400 |
JONES |
2975 |
|
35700 |
MARTIN |
1250 |
1400 |
16400 |
ALLEN |
1600 |
300 |
19500 |
... |
|
|
|
14 rows selected.
3-35

DECODE Functioni
Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement
DECODE(col/expression, search1, result1
[, search2, result2,...,] [, default])
3-36

Usingi the DECODE Functioni
|
|
|
|
|
|
|
|
SQL> SELECT job, sal, |
|
|
|
||
|
2 |
|
DECODE(job, 'ANALYST', |
SAL*1.1, |
|
|
|
3 |
|
|
'CLERK', |
SAL*1.15, |
|
|
4 |
|
|
'MANAGER', SAL*1.20, |
|
|
|
5 |
|
|
|
SAL) |
|
|
6 |
|
REVISED_SALARY |
|
|
|
|
7 |
FROM |
emp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JOB |
|
SAL REVISED_SALARY |
|
|
|
|
--------- --------- -------------- |
|
|
|||
|
PRESIDENT |
5000 |
5000 |
|
|
|
|
MANAGER |
2850 |
3420 |
|
|
|
|
MANAGER |
2450 |
2940 |
|
|
|
|
... |
|
|
|
|
|
14 rows selected.
3-37

Nestingi Functionsi
•• Singlei le--rowr functionsf ti can be nestedt toto any levell l..
•• Nestedt functionsf ti arere evaluatedl t fromfr deepestt levell l toto thet leastl t deep levell l..
F3(F2(F1(col,arg1),arg2),arg3) |
Step 1 = Result 1 |
Step 2 = Result 2
Step 3 = Result 3
3-38

Nestingi Functionsi
|
|
|
|
|
|
SQL> |
SELECT |
ename, |
|
|
2 |
|
NVL(TO_CHAR(mgr),'No Manager') |
|
|
3 |
FROM |
emp |
|
|
4 |
WHERE |
mgr IS NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ENAME |
NVL(TO_CHAR(MGR),'NOMANAGER') |
|
|
---------- |
----------------------------- |
|
|
KING |
No Manager |
|
|
|
|
|
3-39

Summary
Use functions to:
•• Performrf rm calculationsl l ti on datata
•• Modifyify individuali i i l datata itemsit
•• Manipulatei l te outputt t forf r groupsr off rowsr
•• Alterlt r datete formatsf r ts forf r displayi l
•• Convertrt columnl datatypest t
3-40

Practicei Overviewi
•Creating queries that require the use of numeric, character, and date functions
•Creating queries that require the use of numeric, character, and date functions
•Using concatenation with functions
•Using concatenation with functions
•Writing case-insensitive queries to test the usefulness of character functions
•Writing case-insensitive queries to test the usefulness of character functions
•Performing calculations of years and months of service for an employee
•Performing calculations of years and months of service for an employee
•Determining the review date for an employee
•Determining the review date for an employee
3-41

3-42