2 - SQL - Single-Row Functions
2 - SQL - Single-Row Functions
Selected Single-Row
Functions
and Advanced DML & DDL
Chapter Objectives
• Use the UPPER, LOWER, and INITCAP
functions to change the case of field
values and character strings
• Extract a substring using the SUBSTR
function
• Determine the length of a character string
using the LENGTH function
Chapter Objectives
• Use the LPAD and RPAD functions to pad a
string to a desired width
• Use the LTRIM and RTRIM functions to remove
specific characters strings
• Round and truncate numeric data using the
ROUND and TRUNC functions
• Calculate the number of months between two
dates using the MONTHS_BETWEEN function
Chapter Objectives
• Identify and correct problems associated with
calculations involving null values using the NVL
function
• Display dates and numbers in a specific format
with the TO_CHAR function
• Determine the current date setting using the
SYSDATE keyword
• Nest functions inside other functions
• Identify when to use the DUAL table
Terminology
• Function – predefined block of code that
accepts arguments
• Single-row Function – returns one row of
results for each record processed
• Multiple-row Function – returns one result
per group of data processed
Types of Functions
Case Conversion Functions
Alter the case of data stored in a column
or character string
LOWER Function
Used to convert characters to lower-case
letters
UPPER Function
Used to convert characters to upper-case
letters
INITCAP Function
Used to convert characters to mixed-case
Character Manipulation
Functions
Manipulates data by extracting substrings,
counting number of characters, replacing
strings, etc.
SUBSTR Function
Used to return a substring, or portion of a string
LENGTH Function
Used to determine the number of characters in a
string
LPAD and RPAD Functions
Used to pad, or fill in, a character string to a
fixed width
LTRIM and RTRIM Functions
Used to remove a specific string of characters
REPLACE Function
Substitutes a string with another specified string
CONCAT Function
Used to concatenate two character strings
Number Functions
Allows for manipulation of numeric data
ROUND Function
Used to round numeric columns to a stated
precision
TRUNC Function
Used to truncate a numeric value to a specific position
Date Functions
Used to perform date calculations or
format date values
MONTHS_BETWEEN Function
Determines the number of months
between two dates
ADD_MONTHS Function
Adds a specified number of months to a date
NEXT_DAY Function
Determines the next occurrence of a
specified day of the week after a given
date
TO_DATE Function
Converts various date formats to the
internal format (DD-MON-YYYY) used by
Oracle9i
Format Model Elements - Dates
NVL Function
Substitutes a value for a NULL value
NVL2 Function
Allows different actions based on whether a
value is NULL
TO_CHAR Function
Converts dates and numbers to a
formatted character string
Format Model Elements –
Time and Number
Other Functions
• NVL
• NVL2
• TO_CHAR
• DECODE
• SOUNDEX
DECODE Function
Determines action based upon values in a list
SOUNDEX Function
References phonetic representation of
words
Nesting Functions
• One function is used as an argument inside
another function
• Must include all arguments for each function
• Inner function is resolved first, then outer
function
Summary of functions
Single-Row Functions
• Text Functions
lpad, rpad, lower, upper, initcap, length, substr, instr, trim,
concat
• Arithmetic Functions
abs, round, ceil, floor, mod, sign, sqrt, trunc, vsize
• List Functions
greatest, least, decode
• Date Functions
add_months, last_day, months_between, new_time,
next_day, round, trunc
• Conversion Functions
to_char, to_number, to_date
DUAL Table
• Dummy table
• Consists of one column and one row
• Can be used for table reference in the FROM
clause
Dropping a Table
Removing a Sequence
DROP SEQUENCE deptid_seq;
Appendix B: Useful link