Professional Documents
Culture Documents
Chapter 7 Conversion Functions: Oracle 12c Database
Chapter 7 Conversion Functions: Oracle 12c Database
Oracle 12c
Database
Conversion
Functions and
Conditional
Expressions
Copyright @Jamhuriya University 01/24/2022 2
Conversion Functions
Data type
conversion
Implicit data type conversions work according to the rules explained in the next
two slides.
Explicit data type conversions are done by using the conversion functions.
Conversion functions convert a value from one data type to another.
Generally, the form of the function names follows the convention data type TO data
type.
The first data type is the input data type and the second data type is the output
• For expression evaluation, Oracle server can automatically convert the following:
From To
NUMBER VARCHAR2 or CHAR
• SQL provides three functions to convert a value from one data type to another:
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
TO_CHAR converts a datetime data type to a value of VARCHAR2 data type in the format specified by
the format_model.
• Syntax :
TO_CHAR(date, 'format_model')
• The format model:
Must be enclosed with single quotation marks
Is case-sensitive
Element Result
YYYY Full year in numbers
ddspth fourteenth
SELECT last_name,
TO_CHAR(hire_date, 'fmDD Month YYYY')
AS HIREDATE
FROM employees;
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
The Oracle server rounds the stored decimal value to the number of decimal places
provided in the format model.
TO_NUMBER(char[, 'format_model'])
TO_DATE(char[, 'format_model'])
These functions have an fx modifier. This modifier specifies the exact match for the
character argument and date format model of a TO_DATE function.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
SELECT last_name,
UPPER(CONCAT(SUBSTR (LAST_NAME, 1, 8), '_US'))
FROM employees
WHERE department_id = 60;
1. NVL (expr1, expr2) 2. NVL2 (expr1, expr2, expr3) 3. NULLIF (expr1, expr2)
Data types that can be used are date, character, and number. Data types must match:
1. NVL(commission_pct,0) 2. NVL(hire_date,'01-JAN-97’)
…
1 2
1 2
Copyright @Jamhuriya University 01/24/2022 17
Using the NULLIF Function
1
The NULLIF function
SELECT first_name, LENGTH(first_name) "expr1",
compares two expressions. last_name, LENGTH(last_name) "expr2", 2
If they are equal, the FROM
NULLIF(LENGTH(first_name), LENGTH(last_name)) result
employees;
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
1 2 3
If the first expression is not null, the COALESCE function returns that expression; otherwise, it does a COALESCE
of the remaining expressions.
CASE expression
DECODE function
Display the applicable tax rate for each employee in department 80: