
- •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

3
Single-Row Functions

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Describeri variousri typest off functionsf ti availableil le inin SQL
•• Use character,r t r, number,r, and datete functionsf ti inin SELECT statementst t ts
•• Describeri thet use off conversionr i functionsf ti
3-2

SQL Functionsi
Input |
arg 1 |
arg 2 |
arg n |
Function |
Function performs action
Output
Result |
value |
3-3

Two Types of SQL Functionsi
Functions |
Single-row |
functions |
Multiple-row |
functions |
3-4

Singlei le-Row Functionsi
•• Manipulatei l te datata itemsit
•• Acceptt argumentsr ts and returnr t rn one valuel
•• Actt on each rowr returnedr t r
•• Returnt rn one resultr lt perr rowr
•• May modifyify thet datatypet t
•• Can be nestedt
function_name (column|expression, [arg1, arg2,...])
3-5

Singlei le-Row Functionsi
General |
Character |
Single-row |
functions |
Number |
Conversion |
Date |
3-6

Character Functionsi
Character |
functions |
Case conversion |
Character manipulation |
functions |
functions |
LOWER |
CONCAT |
UPPER |
SUBSTR |
INITCAP |
LENGTH |
|
INSTR |
|
LPAD |
3-7

Case Conversioni Functionsi
Convert case for character strings
Function |
|
Result |
LOWER('SQL Course') |
|
sql course |
UPPER('SQL Course') |
|
SQL COURSE |
INITCAP('SQL Course') |
|
Sql Course |
3-8

Usingi Case Conversioni Functionsi
Display the employee number, name, and department number for employee Blake.
SQL> |
SELECT |
empno, ename, deptno |
2 |
FROM |
emp |
3 |
WHERE |
ename = 'blake'; |
no rows selected
|
|
|
|
|
|
SQL> |
SELECT |
empno, ename, deptno |
|
|
2 |
FROM |
emp |
|
|
3 |
WHERE |
LOWER(ename) = 'blake'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
DEPTNO |
|
|
--------- |
---------- --------- |
|
|
|
7698 |
BLAKE |
30 |
|
|
|
|
|
|
3-9

Character Manipulationi l i Functionsi
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 |
3-10

Usingi the Character
Manipulationi l i Functionsi
SQL> SELECT ename, CONCAT (ename, job), LENGTH(ename),
2INSTR(ename, 'A')
|
3 |
FROM |
emp |
|
|
|
|
4 |
WHERE |
SUBSTR(job,1,5) = 'SALES'; |
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ENAME |
CONCAT(ENAME,JOB) |
LENGTH(ENAME) INSTR(ENAME,'A') |
|
||
|
---------- |
------------------- ------------- ---------------- |
|
|||
|
MARTIN |
MARTINSALESMAN |
6 |
2 |
|
|
|
ALLEN |
ALLENSALESMAN |
5 |
1 |
|
|
|
TURNER |
TURNERSALESMAN |
6 |
0 |
|
|
|
WARD |
|
WARDSALESMAN |
4 |
2 |
|
|
|
|
|
|
|
|
3-11