0% found this document useful (0 votes)
27 views

At A Glance: Selected Single-Row Functions

This document provides an overview of chapter 10 which examines several single-row functions in Oracle 12c. The chapter objectives are to learn how to use functions for case conversion, character manipulation, numbers, dates, regular expressions, and other purposes. Examples and tips are provided for using functions like UPPER, LOWER, SUBSTR, ROUND, ADD_MONTHS, and more. Quizzes are included to test understanding of the functions.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

At A Glance: Selected Single-Row Functions

This document provides an overview of chapter 10 which examines several single-row functions in Oracle 12c. The chapter objectives are to learn how to use functions for case conversion, character manipulation, numbers, dates, regular expressions, and other purposes. Examples and tips are provided for using functions like UPPER, LOWER, SUBSTR, ROUND, ADD_MONTHS, and more. Quizzes are included to test understanding of the functions.

Uploaded by

brd83477
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Oracle 12c: SQL 10-1

Chapter 10
Selected Single-Row Functions

At a Glance
Instructor’s Notes
♦ Chapter Overview

♦ Chapter Objectives

♦ Instructor Notes

♦ Troubleshooting Tips

♦ Quick Quizzes

♦ Discussion Questions

♦ Key Terms
Oracle 12c: SQL 10-2

Chapter Overview
The focus of this chapter is to examine several single-row functions. Single-row functions return
one row of results for each record processed. Such functions can be used to perform case
conversions, date and numeric calculations, and various other data manipulations. The DUAL
table can be used to practice function usage.

Chapter Objectives
After completing this chapter, you should be able to do the following:

♦ Use the UPPER, LOWER, and INITCAP functions to change the case of field values
and character strings
♦ Manipulate character substrings with the SUBSTR and INSTR functions
♦ Nest functions inside other functions
♦ Determine the length of a character string using the LENGTH function
♦ Use the LPAD and RPAD functions to pad a string to a certain width
♦ Use the LTRIM and RTRIM functions to remove specific character strings
♦ Substitute character string values with the REPLACE and TRANSLATE functions
♦ Combine character strings with the CONCAT function
♦ Round and truncate numeric data using the ROUND and TRUNC functions
♦ Return the remainder only of a division operation using the MOD function
♦ Use the ABS function to set numeric values as positive
♦ Use the POWER function to raise a number to a specified power
♦ Calculate the number of months between two dates using the MONTHS_BETWEEN
function
♦ Manipulate date data using the ADD_MONTHS, NEXT_DAY, LAST_DAY, and
TO_DATE functions
♦ Differentiate between CURRENT_DATE and SYSDATE values
♦ Extend pattern matching capabilities with regular expressions
♦ Identify and correct problems associated with calculations involving NULL values
using the NVL function
♦ Manipulate NULL values with the NVL2 and NULLIF functions
♦ Display dates and numbers in a specific format using the TO_CHAR function
♦ Perform condition processing similar to an IF statement with the DECODE function
and CASE Expression
♦ Use the SOUNDEX function to identify character phonetics
♦ Convert string values to numeric with the TO_NUMBER function
♦ Use the DUAL table to test functions
Oracle 12c: SQL 10-3

Instructor Notes

Case Conversion Functions


Case conversion functions, such as LOWER, UPPER, and INITCAP, are used to alter the case of
the character strings. When used in a SELECT clause, these functions only change the way the
data is displayed, not how it is stored in the database table. These functions are used frequently
to circumvent the case sensitivity issues of conditional expressions.

Troubleshooting Tip To quickly present how a function works, use examples with static
values and reference the DUAL table. Afterwards, have student
create queries using data contained in the JustLee Books database.

Quick Quiz
1. Which function is used to convert characters to mixed case characters?
ANSWER: INITCAP

2. Which function can be used to convert characters to all uppercase characters?


ANSWER: UPPER

3. Which function can be used to convert uppercase characters to lowercase characters?


ANSWER: LOWER

4. What happens if a case conversion function is used in the SELECT clause when data is
retrieved?
ANSWER: The results are displayed in the case indicated by the function.

Character Manipulation Functions


Character manipulation functions can be used to extract portions of a string, determine the length
of a string, or even change the appearance of data. These types of functions can be used to
prepare data for inclusion in a preprinted form or to retrieve information needed for utility or
database maintenance purposes. One very commonly used function is the RPAD function to add
asterisks to fill in blank spaces on checks. This section introduces SUBSTR, INSTR, LENGTH,
LPAD/RPAD, LTRIM/RTRIM, REPLACE, TRANSLATE, and CONCAT.
Oracle 12c: SQL 10-4

Troubleshooting Tip There are numerous functions supported by Oracle 12c. Have the
students create an account for the Oracle Technology Network and
view some of the function documentation at the Web site. The
SQL reference book contains an entire chapter on single-row
functions.

Troubleshooting Tip Demonstrate that various types of single-row functions can be


nested. For example, the INSTR function can be nested in a
SUBSTR function.

Quick Quiz
1. What is the purpose of the SUBSTR function?
ANSWER: It is used to return a portion of a string.

2. Which function can be used to determine the length of a string?


ANSWER: LENGTH function

3. Which function can be used to fill in blank spaces in a preprinted form?


ANSWER: LPAD or RPAD functions

4. How many character strings can be concatenated with the CONCAT function?
ANSWER: 2

5. Which function can be used to substitute one character string for another character string?
ANSWER: REPLACE function

Number Functions
Although the majority of functions that are used to perform calculations are multiple-row
functions, there are some single-row functions that are used exclusively with numeric data. The
ROUND, TRUNC, MOD, and ABS functions are addressed in this section. The ROUND and
TRUNC function are used to alter the precision or scale of a numeric value. Remember when
using either of these functions, a negative value represents digits to the left of a decimal position,
and a positive value represents digits to the right. The MOD function returns the remainder
portion of a division result, and the ABS function calculates the absolute value of the number
provided.
Oracle 12c: SQL 10-5

Quick Quiz
1. If a –3 is assigned as the position to which a value should be rounded by the ROUND
function, on which side of the decimal point will the rounding occur?
ANSWER: left side

2. What is the difference between rounding a number and truncating a number?


ANSWER: With rounding, a value is affected by the number preceding the indicated
position, but when truncating, everything beyond the indicated position is simply set to
zero.

3. Can the ROUND function be used with characters strings?


ANSWER: No

Date Functions
Oracle 12c data functions can be used to perform calculations with date values or to format an
inputted date value to a format that can be interpreted by the server. The TO_DATE function is
widely used in application programs to allow users to enter dates in a variety of formats. The
function converts the entered date to Oracle 12c internal storage format. The
MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, TO_DATE, ROUND, TRUNC, and
CURRENT_DATE functions are addressed in this section.

Troubleshooting Tip Highlight that the ROUND and TRUNC functions can be used
with both dates and numeric data.

Quick Quiz
1. What is a Julian date?
ANSWER: The number of days that have passed since January 1, 4712, B.C.

2. What is a format model?


ANSWER: The format structure indicating the position and context of each data element

3. How do you indicate that the month is spelled out in a format model?
ANSWER: Use the MONTH element

4. If a ROUND function is nested inside an ADD_MONTHS function, which is already


nested inside a TO_CHAR function, which function is resolved first?
ANSWER: The ROUND function
Oracle 12c: SQL 10-6

5. If a ROUND function is nested inside an ADD_MONTHS function, which is already


nested inside a TO_CHAR function, how many closing parentheses will be required?
ANSWER: 3

Regular Expressions
Regular expressions allow the identification of complex patterns within data. Many of the regular
expressions extend the capabilities of existing single-row functions. For example, the
REGEXP_LIKE function provides more functionality than the LIKE operator introduced earlier.
The REGEXP_LIKE and REGEXP_SUBSTR functions are introduced in this section.

Other Functions
This section covers the NVL, NVL2, TO_CHAR, DECODE, SOUNDEX, and TO_NUMBER
functions. The NVL and NVL2 functions are widely used when NULL values may affect the
results of a query. The NVL function allows another value to be substituted for a NULL during
calculations. However, it cannot be used to display a text message in place of the NULL. The
NVL2 function is used for this purpose. The TO_CHAR function can be used with an extensive
format model to display numeric and date values.

The DECODE function is similar to an IF/THEN statement available in most programming


languages. The SOUNDEX function assists in searching data based on phonetic pronunciation.
The TO_NUMBER function enables string data to be recognized as a numeric value for purposes
of a calculation.

Troubleshooting Tip Highlight again that functions can be nested. For example, the
ADD_MONTHS function can be nested in a TO_CHAR
function.

Quick Quiz
1. A NULL value can be replaced by a text message, while displaying a different message if
there is not a NULL value, using which function?
ANSWER: NVL2 function

2. Which function should be used to replace a NULL value with a numeric value for
calculation purposes?
ANSWER: NVL

3. Which function should be used to display the current time according to the computer?
ANSWER: TO_CHAR
Oracle 12c: SQL 10-7

4. Which format model element is used to indicate that minutes should be included in the
display?
ANSWER: MI

5. What is a floating dollar sign?


ANSWER: A dollar sign whose exact position will vary depending on the number of
digits displayed

DUAL Table
The DUAL table is a dummy table created by default in Oracle 12c. The table consists of one
column and one row and stores no data. Since a SELECT statement must reference a table in the
FROM clause, this table is frequently used to return values from a function when the value is not
already stored in a database table.

Quick Quiz
1. How many columns are in the DUAL table?
ANSWER: 1

2. How many rows are in the DUAL table?


ANSWER: 1

Discussion Questions
1. Would it be easier to simply store the necessary manipulated data in the database table
rather than use functions? Why or why not?

2. Is there an appropriate time to use the TO_DATE function rather than the TO_CHAR
function?

Key Terms
argument — Values listed within parentheses in a function.

case conversion functions — Allow a user to temporarily alter the case of data stored in a
column or character string.

character functions — Used to change the case of characters or manipulate characters.


Oracle 12c: SQL 10-8

function — A named PL/SQL block or predefined block of code that accepts zero or more input
parameters and returns one value.

Julian date — Represents the number of days that have passed between a specified date and
January 1, 4712, B.C.

manipulation functions — Allow the user to control data (e.g., determine the length of a string,
extract portions of a string) to yield a desired query output.

single-row functions — Return one row of results for each record processed.

You might also like