Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter 10
Selected Single-Row Functions
Jason C. H. Chen, Ph.D.
Professor of MIS
School of Business
Gonzaga University
Spokane, WA 99258 USA
chen@jepson.gonzaga.edu
Dr. Chen, Oracle Database System (Oracle)
Objectives
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 characters
strings
Substitute character string values with the REPLACE and
TRANSLATE functions
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
Dr. Chen, Oracle Database System (Oracle)
Objectives (continued)
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
Display dates and numbers in a specific format with the TO_CHAR
function
Perform condition processing similar to an IF statement with the
DECODE function
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
Learn PL/SQL Procedure Language / SQL
Dr. Chen, Oracle Database System (Oracle)
5. Other functions
NVL, TO_CHAR, CASE, TO_NUMBER, DUAL table
Manipulating Character
Strings with PL/SQL
To change case, use UPPER, LOWER, INITCAP
INSTR function searches a string for a specific
substring and return the starting position
start_position := INSTR(original_string, substring,
starting_pos, occurrence#);
/* the first two arguments are required, the last two
arguments are optional */
SUBSTR function extracts a specific number of
characters from a character string, starting at a given
point:
extracted_string := SUBSTR(string_variable,
starting_point, number_of_characters);
LOWER Function
Used to convert characters to lowercase letters
UPPER Function
Used to convert characters to uppercase letters
It can be used in the same way as the LOWER
function
To affect the display of characters, it is used in
a SELECT clause
To modify the case of characters for a search
condition, it is used in a WHERE clause
The syntax for the UPPER function is UPPER(c)
Where c is the character string or field to be
converted into uppercase characters
Dr. Chen, Oracle Database System (Oracle)
10
11
INITCAP Function
Used to convert character strings to mixed case,
which each word beginning with a capital letter.
12
13
SUBSTR Function
Used to return a substring, or portion of a string
SUBSTR (c, p, l)
Where
c: the character
string
p: the beginning
character position,
l: the length of
the string to
return in the
query result.
-p: backward p
position
Figure 10-6 Comparison of SUBSTR arguments
Dr. Chen, Oracle Database System (Oracle)
14
INSTR Function
start_position := INSTR(original_string, substring, starting_pos, occurrence#);
/* the first two arguments are required, the last two arguments are optional */
14
Why?
15
What is PL/SQL?
Procedural programming Language
Uses detailed instructions
Processes statements sequentially
16
Why PL/SQL?
ORACLE
(programming language
components)
SQL
PL/SQL
D.B.
GUI
Components
(reports, forms,
graphics etc.)
Dr. Chen, Oracle Database System (Oracle)
17
Purpose
Examples of
Commands
Can Be Used
in PL/SQL
CREATE, ALTER,
DROP, GRANT,
REVOKE
No
Data
Manipulation
Language
(DML)
Transaction
control
commands
Organize DML
commands into
logical
transactions
COMMIT,
ROLLBACK,
SAVEPOINT
Yes
Yes
18
Fundamentals of PL/SQL
Full-featured programming language
Execute using Oracle 11g utilities
SQL*Plus
Forms Builder
An interpreted language
Semicolon ends each command
Reserved words
Type in editor, execute in SQL*Plus
19
Capitalization
Example
Reserved Words
Uppercase
BEGIN, DECLARE
Built-in functions
Uppercase
COUNT, TO_DATE
Uppercase
VARCHAR2, NUMBER
SQL commands
Uppercase
SELECT, INSERT
Database objects
Lowercase
student, fid
Variable names
Lowercase
s_first_name, faculty_id
Mixed case
Student_F_Name, Faculty_ID
20
From 1 to 30 characters
Only alphanumeric characters, and special
characters ($ _ #)
Must begin with a letter and can not contain
blank spaces or hyphens
21
22
Language Elements:
PL/SQL Data Types
(we will focus on this type)
References a single value
Scalar
Composite
References a data structure
Reference
References a specific database item (e.g., %TYPE,
%ROWTYPE)
LOB
References a large binary object (e.g., graphs, video)
23
24
25
Usage
Sample Declaration
VARCHAR2
Variable-length character
strings
student_name
VARCHAR2(30);
CHAR
Fixed-length character
strings
student_gender CHAR(1);
NUMBER
Floating, fixed-point, or
integer number
current_price NUMBER(5,2);
LONG
evaluation_summary LONG;
DATE
Dates
todays_date DATE;
BOOLEAN
True/False values
order_flag BOOLEAN;
%TYPE
c_address
customer.customer_address%; TYPE
%ROWTYPE
cust_order_record
cust_order% ROWTYPE;
26
Block Structure
PL/SQL is a block
structured language.
That just means everything
you do is done in terms of
blocks of code.
EXCEPTION
Comments:
<exception/error
handling code>;
END;
27
Comment Statements
There are two types of comments in PL/SQL:
Single-line and multi-line.
Single Line:
x := 4; -- assign 4 to the variable x
Multi-line:
/* Assign 4 to
the variable x */
x := 4;
Dr. Chen, Oracle Database System (Oracle)
28
Assignment Statements
29
30
31
Description
Converts a character string
to a date
Example
TO_DATE(07/14/01,MM/DD/YY);
TO_NUMBER(2);
32
Logic error:
Program runs but results in an incorrect result
Caused by mistake in program
RUN-TIME error
Dr. Chen, Oracle Database System (Oracle)
33
34
35
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 (covered in the
next chapter)
36
Types of Functions
Table 10-1
37
38
39
40
Your Turn
PART II
Study yourself
Learning to learn and learning to change
41
Nesting Functions
42
LENGTH Function
Used to determine the number of characters in a string
Figure 10-10
Dr. Chen, Oracle Database System (Oracle)
LTRIM
Figure 10-12
Dr. Chen, Oracle Database System (Oracle)
REPLACE Function
Figure 10-13
Dr. Chen, Oracle Database System (Oracle)
TRANSLATE Function
Figure 10-14
46
CONCAT Function
Figure 10-15
Dr. Chen, Oracle Database System (Oracle)
Number Functions
Allow for manipulation of numeric data
ROUND
TRUNC
MOD
ABS
48
ROUND Function
Used to round numeric columns to a stated precision
Figure 10-16
49
TRUNC Function
Used to truncate a numeric value to a specific position
Figure 10-17
50
MOD Function
Figure 10-18
51
ABS Function
Figure 10-19
52
Date Functions
Used to perform date calculations or format date values
Subtract date for number of days difference
Figure 10-20
53
MONTHS_BETWEEN Function
Figure 10-21
Dr. Chen, Oracle Database System (Oracle)
ADD_MONTHS Function
Figure 10-22
Dr. Chen, Oracle Database System (Oracle)
NEXT_DAY Function
Figure 10-23
Dr. Chen, Oracle Database System (Oracle)
TO_DATE Function
Figure 10-25
Dr. Chen, Oracle Database System (Oracle)
Table 10-2
Dr. Chen, Oracle Database System (Oracle)
ROUND Function
Figure 10-26
59
TRUNC Function
Figure 10-27
60
TO_CHAR Function
Converts dates and numbers to a formatted
character string
Figure 10-39
Dr. Chen, Oracle Database System (Oracle)
TO_NUMBER Function
Figure 10-43
62
DUAL Table
Dummy table
Consists of one column and one row
Can be used for table reference in the FROM
clause
63
Using DUAL
Figure 10-44
64
Summary
Single-row functions return a result for each row or record
processed
Case conversion functions such as UPPER, LOWER, and
INITCAP can be used to alter the case of character strings
Character manipulation functions can be used to extract
substrings (portions of a string), identify the position of a
substring in a string, replace occurrences of a string with
another string, determine the length of a character string, and
trim spaces or characters from strings
Nesting one function within another allows multiple
operations to be performed on data
65
Summary (continued)
Simple number functions such as ROUND and TRUNC can
round or truncate a number on both the left and right side of a
decimal
The MOD function is used to return the remainder of a
division operation
Date functions can be used to perform calculations with dates
or to change the format of dates entered by a user
Regular expressions enable complex pattern matching
operations
The NVL, NVL2, and NULLIF functions are used to address
problems encountered with NULL values
66
Summary (continued)
The TO_CHAR function lets a user present numeric data and
dates in a specific format
The DECODE function allows an action to be taken to be
determined by a specific value
The searched CASE expression enables you to evaluate
conditions to determine the resulting value
The SOUNDEX function looks for records based on the
phonetic representation of characters
The DUAL table can be helpful when testing functions
67