SQL04
SQL04
4-2
Lesson Agenda
4-3
Conversion Functions
Data type
conversion
4-4
Implicit Data Type Conversion
4-5
Implicit Data Type Conversion
4-6
Explicit Data Type Conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
4-7
Explicit Data Type Conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
4-8
Lesson Agenda
4 - 10
Using the TO_CHAR Function with Dates
TO_CHAR(date, 'format_model')
4 - 11
Elements of the Date Format Model
Element Result
YYYY Full year in numbers
YEAR Year spelled out (in English)
MM Two-digit value for the month
MONTH Full name of the month
4 - 12
Elements of the Date Format Model
HH24:MI:SS AM 15:45:32 PM
ddspth fourteenth
4 - 14
Using the TO_CHAR Function with Dates
SELECT last_name,
TO_CHAR(hire_date, 'fmDD Month YYYY')
AS HIREDATE
FROM employees;
4 - 16
Using the TO_CHAR Function with Numbers
TO_CHAR(number, 'format_model')
These are some of the format elements that you can use with
the TO_CHAR function to display a number value as a character:
Element Result
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 comma as a thousands indicator
4 - 17
Using the TO_CHAR Function with Numbers
4 - 19
Using the TO_NUMBER and TO_DATE Functions
TO_NUMBER(char[, 'format_model'])
4 - 20
Using the TO_CHAR and TO_DATE Function
with RR Date Format
To find employees hired before 1990, use the RR date format,
which produces the same results whether the command is run
in 1999 or now:
4 - 22
Lesson Agenda
4 - 23
Nesting Functions
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
4 - 24
Nesting Functions
SELECT last_name,
UPPER(CONCAT(SUBSTR (LAST_NAME, 1, 8), '_US'))
FROM employees
WHERE department_id = 60;
4 - 25
Lesson Agenda
4 - 26
General Functions
The following functions work with any data type and pertain to
using nulls:
• NVL (expr1, expr2)
• NVL2 (expr1, expr2, expr3)
• NULLIF (expr1, expr2)
• COALESCE (expr1, expr2, ..., exprn)
4 - 27
NVL Function
4 - 28
Using the NVL Function
1
SELECT last_name, salary, NVL(commission_pct, 0),
(salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SAL 2
FROM employees;
…
1 2
4 - 29
Using the NVL2 Function
1 2
4 - 30
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
4 - 31
Using the COALESCE Function
4 - 32
Using the COALESCE Function
…
4 - 33
Lesson Agenda
4 - 35
Conditional Expressions
4 - 36
CASE Expression
4 - 37
Using the CASE Expression
…
4 - 38
DECODE Function
4 - 39
Using the DECODE Function
4 - 40
Using the DECODE Function
4 - 41
Summary
4 - 42
Practice 4: Overview
4 - 43