LAB 4 - Conversion Function and Conditional Expression
LAB 4 - Conversion Function and Conditional Expression
HR
After completing this lesson, you should be able to do the following:
HR
Accept character input & can return
Character both character & number values
NVL,
NVL2,
NULLIF, Single-row
General Number
COALESC functions
E,CASE,
DECODE Accept numeric input &
return numeric values
HR
For databases, format and display changes are done using conversion functions.
These functions are able to display numbers as local currency, format dates in a
variety of formats, display time to the second, and keep track of what century a
date refers to.
This data type conversion can be done implicitly by the Oracle server or explicitly
by the user
HR
In SQL, there are several different data types. These data types define the domain
of values that each column can contain.
VARCHAR2: Used for character data of variable length, including numbers, dashes,
and special characters.
CHAR: Used for text and character data of fixed length, including numbers, dashes,
and special characters.
NUMBER: Used to store variable-length numeric data. No dashes, text, or other
nonnumeric data are allowed. Currency is stored as a number data type.
DATE: Used for date and time values. Internally, Oracle stores dates as numbers
and, by default, DATE information is displayed as DD/Mon/YYYY (for example,
23/Oct/2013).
HR
Type conversion
HR
DATE to VARCHAR2 Conversion
It is often desirable to convert a date from its default DD/MON/YYYY format to
another format specified by you.
The function to accomplish this task is:
TO_CHAR (date column name, 'format model you specify’)
TO_CHAR
The 'format model' must be enclosed in single quotation marks and is case-
sensitive.
Separate the date value from the format model with a comma.
Any valid date format element can be included.
HR
Format model that can be used with
date to character conversion
Use sp to spell out a number.
Use th to have the number appear
as an ordinal. (1st, 2nd, 3rd, and so
on)
TO_CHAR
HR
SELECT hire_Date, TO_CHAR(hire_date, 'Month dd, YYYY')
FROM employees;
HR
SELECT hire_date, TO_CHAR(hire_date, 'fmDay, ddthsp "of" Month, Year')
FROM employees;
SELECT last_name,
TO_CHAR(hire_date, 'fmDdspth "of" Month YYYY fmHH:MI:SS AM')HIREDATE
FROM employees;
HR
NUMBER to VARCHAR2 Conversion
To add formatting, you first need to convert the number to a character format.
HR
SELECT salary, TO_CHAR(salary, '$99,999.00’)
FROM employees
WHERE last_name = 'Ernst';
HR
CHARACTER to NUMBER Function
It is often desirable to convert a character string to a number. The function for this
conversion is:
The format model is optional, but should be included if the character string being
converted contains any characters other than numbers.
TO_NUMBER
HR
CHARACTER to DATE Function
To convert a character string to a date format, use:
TO_DATE('character string', 'format model’)
For example:
TO_DATE
HR
The fx modifier rules are:
When making a character-to-date conversion, the fx (format exact) modifier
specifies exact matching for the character argument and the date format model.
In the following example, note that "May10" has no space between ''May" and "10."
The fx format model matches the character argument as it also has no space
between "Mon" and "DD."
HR
Punctuation and quoted text in the character argument must match the
corresponding parts of the format model exactly (except for case).
The character argument cannot have extra blanks.
Without fx, the Oracle Server ignores extra blanks.
Numeric data in the character argument must have the same number of digits as
the corresponding element in the format model.
Without fx, numbers in the character argument can omit leading zeros.
HR
HR
RR or YY Date Format
All date data should now be stored using four-digit years (YYYY).
Some legacy databases however may still use the two-digit (YY) format.
It has not been that long since the century changed from 1900 to 2000.
Along with this change came considerable confusion as to whether a date written
as 02/Jan/98 would be interpreted as January 2, 1998 or January 2, 2098.
HR
HR
Single-row functions can be nested to any level.
Nested functions are evaluated from the deepest level to the least deep level.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
HR
SELECT last_name,
UPPER(CONCAT(SUBSTR (LAST_NAME, 1, 8), '_US'))
FROM employees
WHERE department_id = 60;
HR
At the beginning of the course, the term "null" was introduced.
Null is the value that is unavailable, unassigned, unknown, or inapplicable.
Oracle has four general functions that pertain to the use of null values.
The four functions are:
NVL
NVL2
NULLIF
COALESCE
HR
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)
Function Description
NVL Converts a null value to an actual value
NVL2 If expr1 is not null, NVL2 returns expr2. if expr1 is null, NVL2
returns expr3. The arguments expr1 can have any data type
NULLIF Compares two expressions and returns null if they are equal;
returns the first expression if they are not equal
COALESCE Returns the first non-null expression in the expression list
HR
NVL Function
HR
NVL2 Function
HR
NULLIF Function
HR
COALESCE Function
HR
Being able to make decisions is essential in data modeling.
Modelers have to decide which business functions need to be modeled and which
do not.
The data-modeling process requires that designers analyze information to identify
entities, resolve relationships, and select attributes.
A typical decision could be:
– IF a business needs to track data over time, THEN time may need to be an entity
or ELSE time should be an attribute.
HR
CASE and DECODE are examples of one of these differences.
CASE is an ANSI/ISO 99 SQL 99 compliant statement.
DECODE is an Oracle Proprietary statement.
Both statements return the same information using different syntax.
HR
CASE CONVERSION
The CASE expression basically does the work of an IF-THEN- ELSE statement.
Data types of the CASE, WHEN, and ELSE expressions must be the same.
The syntax for a CASE expression is:
HR
The query checks the department_id.
IF it is 90, then return 'Management'
IF it is 80, then return 'Sales'
IF it is 60, then return 'It'
ELSE return 'Other dept.’
SELECT last_name,
(CASE department_id WHEN 90 THEN 'Management'
WHEN 80 THEN 'Sales'
WHEN 60 THEN 'IT'
ELSE 'Other dept.' END) AS "Department"
FROM employees;
HR
No specified case:
SELECT last_name,salary,
(CASE WHEN salary<5000 THEN 'Low'
WHEN salary<10000 THEN 'Medium'
WHEN salary<20000 THEN 'Good'
ELSE 'Excellent'
END) qualified_salary
FROM employees;
HR
DECODE Function
The DECODE function evaluates an expression in a similar way to the IF-THEN-
ELSE logic.
DECODE compares an expression to each of the search values.
The syntax for DECODE is:
If the default value is omitted, a null value is returned where a search value does
not match any of the values
HR
SELECT last_name, DECODE(department_id, 90, 'Management',
80, 'Sales',
60, 'It',
'Other dept.') AS "Department"
FROM employees;
HR
SELECT last_name, salary,
DECODE (TRUNC(salary/2000, 0),
0, 0.00,
1, 0.09,
2, 0.20,
3, 0.30,
4, 0.40,
5, 0.42,
6, 0.44,
0.45) TAX_RATE
FROM employees
WHERE department_id = 80;
HR
Accept character input & can return
Character both character & number values
NVL,
NVL2,
NULLIF, Single-row
General Number
COALESC functions
E,CASE,
DECODE Accept numeric input &
return numeric values
HR
Oracle Academy, Database Programming with SQL
HR