Database Systems Lecture5 Part 1
Database Systems Lecture5 Part 1
Conversion Functions
Data type
conversion
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
Implicit Data Type Conversion
From To
NUMBER VARCHAR2 or CHAR
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
Using the TO_CHAR Function with Dates
TO_CHAR(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
…
Using the TO_CHAR Function with Numbers
TO_CHAR(number, 'format_model')
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
Using the TO_CHAR Function with Numbers
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
Nesting Functions
SELECT last_name,
UPPER(CONCAT(SUBSTR (LAST_NAME, 1, 8), '_US'))
FROM employees
WHERE department_id = 60;
General Functions
› NVL(commission_pct,0)
› NVL(hire_date,'01-JAN-97')
› NVL(job_id,'No Job Yet')
Using the NVL Function
…
1 2
Using the NVL2 Function
1 2
Using the NULLIF Function
The NULLIF function compares two expressions. If they are equal,
the function returns a null. If they are not equal, the function returns
the first expression. However, you cannot specify the literal NULL for
the first expression.
Syntax
NULLIF (expr1, expr2)
In the syntax:
➢ NULLIF compares expr1 and expr2. If they are equal, then
the function returns null. If they are not, then the function
returns expr1. However, you cannot specify the literal NULL
for expr1.
In the example shown in the following slide, the length of the first
name in the EMPLOYEES table is compared to the length of the last
name in the EMPLOYEES table. When the lengths of the names are
equal, a null value is displayed. When the lengths of the names are
not equal, the length of the first name is displayed.
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
Using the COALESCE Function
…
References
➢ Oracle Slides