SQL Fundamentals Lesson#5 Functions Single Row II CH#5 Day 06 07
SQL Fundamentals Lesson#5 Functions Single Row II CH#5 Day 06 07
SQL Fundamentals
Lesson#5 -
Single Row functions - Conversion Functions
Objectives
• Describe the various types of conversion functions that are available in SQL
• Use the TO_CHAR, TO_NUMBER, and TO_DATE conversion functions
• Apply conditional expressions in a SELECTstatement
Page 2 of 24
Conversion Functions
However, the Oracle server internally converts such data types to Oracle data types.
In some cases, the Oracle server receives data of one data type where it expects
data of a different data type. When this happens, the Oracle server can automatically
convert the data to the expected data type. This data type conversion can be done
implicitly by the Oracle server or explicitly by the user.
Implicit data type conversions work according to the rules explained in the following
slides.
Explicit data type conversions are performed 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 datatypeTOdatatype. The first data
type is the input data type and the second data type is the output.
Page 3 of 24
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
Page 4 of 24
From To
NUMBER VARCHAR2 or CHAR
DATE VARCHAR2 or CHAR
Page 5 of 24
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
MON Three-letter abbreviation of the month
DY Three-letter abbreviation of the day of the week
DAY Full name of the day of the week
DD Numeric day of the month
Page 8 of 24
HH24:MI:SS AM 15:45:32 PM
ddspth fourteenth
Page 9 of 24
SELECT last_name,
FROM employees;
Page 10 of 24
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
Page 11 of 24
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.
Example
Display the name and hire date for all employees who started on May 24, 2007. There
are two spaces after the month May and before the number 24 in the following
example. Because the fx modifier is used, an exact match is required and the spaces
after the word May are not recognized:
SELECT last_name, hire_date
FROM employees
WHERE hire_date = TO_DATE('May 24, 2007', 'fxMonth DD, YYYY');
The resulting error output looks like this:
To see the output, correct the query by deleting the extra space between ‘May’ and ‘24’.
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)
These functions work with any data type and pertain to the use of null
values in the expression list.
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 argument expr1can 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
Page 14 of 24
NVLFunction
1 2
Page 16 of 24
…
1 2 3
Page 17 of 24
Conditional Expressions
CASEExpression
END
In a simple CASE expression, the Oracle server searches for the first WHEN ... THEN
pair for which expr is equal to comparison_expr and returns return_expr. If none of the
WHEN ... THEN pairs meet this condition, and if an ELSE clause exists, the Oracle
server returns else_expr. Otherwise, the Oracle server returns a null. You cannot
specify the literal NULLfor all the return_exprs and the else_expr.
The expressions exprand comparison_exprmust be of the same data type, which can be
CHAR, VARCHAR2, NCHAR, or NVARCHAR2,NUMBER,BINARY_FLOAT, or
BINARY_DOUBLEor
must all have a numeric data type. All of the return values (return_expr) must be of the
same data type.
Page 21 of 24
Searched CASEExpression
CASE
END
SELECT last_name,salary,
END) qualified_salary
FROM employees;
Page 23 of 24
DECODEFunction
'ST_CLERK', 1.15*salary,
'SA_REP', 1.20*salary,
salary)
REVISED_SALARY
FROM employees;
END ---------------------------------