Curso de Desarrollo de Aplicaciones Corporativas
Curso de Desarrollo de Aplicaciones Corporativas
Desarrollo de Aplicaciones
Corporativas
3-1
3
Single-Row Functions
Objectives
3-3
SQL Functions
Input Output
Function
arg 1 Function
performs action
arg 2
Result
value
arg n
3-4
Two Types of SQL Functions
Functions
Single-row Multiple-row
functions functions
3-5
Single-Row Functions
3-6
Single-Row Functions
Character
General Number
Single-row
functions
Conversion Date
3-7
Character Functions
Character
functions
Case-manipulation Character-manipulation
functions functions
LOWER CONCAT
UPPER SUBSTR
INITCAP LENGTH
INSTR
LPAD | RPAD
TRIM
REPLACE
3-8
Case Manipulation Functions
Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
3-10
Using Case Manipulation Functions
SELECT
SELECT employee_id,
employee_id, last_name,
last_name, department_id
department_id
FROM
FROM employees
employees
WHERE
WHERE last_name
last_name == 'higgins';
'higgins';
no
no rows
rows selected
selected
3-11
Character-Manipulation Functions
Function Result
CONCAT('Hello', 'World') HelloWorld
SUBSTR('HelloWorld',1,5) Hello
LENGTH('HelloWorld') 10
INSTR('HelloWorld', 'W') 6
LPAD(salary,10,'*') *****24000
RPAD(salary, 10, '*') 24000*****
TRIM('H' FROM 'HelloWorld') elloWorld
3-12
Using the Character-Manipulation
Functions
1
1 2 3
3-13
Number Functions
3-14
Using the ROUND Function
1 2
1 2 3
DUAL is a dummy table you can use to view results
from functions and calculations.
3-15
Using the TRUNC Function
1 2
1 2 3
3-16
Using the MOD Function
3-17
Working with Dates
3-18
Working with Dates
3-19
Arithmetic with Dates
3-20
Using Arithmetic Operators
with Dates
3-21
Date Functions
Function Description
3-22
Using Date Functions
• MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
19.6774194
• NEXT_DAY ('01-SEP-95','FRIDAY')
'08-SEP-95'
• LAST_DAY('01-FEB-95') '28-FEB-95'
3-23
Using Date Functions
3-24
Practice 3, Part One: Overview
3-25
Explicit Data Type Conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
3-26
Explicit Data Type Conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
3-27
Using the TO_CHAR Function with Dates
TO_CHAR(date,
TO_CHAR(date, 'format_model')
'format_model')
The format model:
• Must be enclosed in single quotation marks and is case
sensitive
• Can include any valid date format element
• Has an fm element to remove padded blanks or
suppress leading zeros
• Is separated from the date value by a comma
3-29
Elements of the Date Format Model
3-30
Elements of the Date Format Model
ddspth fourteenth
3-32
Using the TO_CHAR Function with Dates
SELECT last_name,
TO_CHAR(hire_date, 'fmDD Month YYYY')
AS HIREDATE
FROM employees;
3-34
Using the TO_CHAR Function with
Numbers
TO_CHAR(number,
TO_CHAR(number, 'format_model')
'format_model')
These are some of the format elements you can use
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
3-35
Using the TO_CHAR Function with Numbers
3-36
Using the TO_NUMBER and TO_DATE
Functions
TO_DATE(char[,
TO_DATE(char[, 'format_model'])
'format_model'])
3-37
Nesting Functions
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
3-38
Nesting Functions
SELECT last_name,
NVL(TO_CHAR(manager_id), 'No Manager')
FROM employees
WHERE manager_id IS NULL;
3-39
General Functions
3-40
NVL Function
3-41
Using the NVL Function
1 2
3-42
Using the NVL2 Function
1 2
3-43
Using the NULLIF Function
1
SELECT first_name, LENGTH(first_name) "expr1",
last_name, LENGTH(last_name) "expr2",
2
NULLIF(LENGTH(first_name), LENGTH(last_name)) result 3
FROM employees;
1 2 3
3-44
Using the COALESCE Function
3-45
Using the COALESCE Function
SELECT last_name,
COALESCE(commission_pct, salary, 10) comm
FROM employees
ORDER BY commission_pct;
3-46
Conditional Expressions
3-47
The CASE Expression
CASE
CASE expr
expr WHEN
WHEN comparison_expr1
comparison_expr1 THEN
THEN return_expr1
return_expr1
[WHEN
[WHEN comparison_expr2
comparison_expr2 THEN
THEN return_expr2
return_expr2
WHEN
WHEN comparison_exprn
comparison_exprn THEN
THEN return_exprn
return_exprn
ELSE
ELSE else_expr]
else_expr]
END
END
3-48
Using the CASE Expression
3-49
The DECODE Function
DECODE(col|expression,
DECODE(col|expression, search1,
search1, result1
result1
[,
[, search2,
search2, result2,...,]
result2,...,]
[,
[, default])
default])
3-50
Using the DECODE Function
3-51
Using the DECODE Function
3-52
Summary
3-53
Practice 3, Part Two: Overview
3-54
3-55
3-57