Week 9 - Single Row Function
Week 9 - Single Row Function
Functions make the basic query block more powerful, and they are
Single-Row
used to manipulate data values.
Function
After completing this lesson, the student should be able to:
• Describe the various types of functions available in SQL
• Use the character, number, and date functions in SELECT
statements
Single-row functions:
• Use to manipulate data items
• Can accept arguments that may return one value
• Act on each row that is returned
• Can return one result per row
• Can be used to modify the data type
• Arguments can be nested
• Can accept arguments that can be a column or an expression
• Syntax:
SELECT Function_name (arguments1,2 [expression])
FROM tbl_name;
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Where:
function_name - Is the name of the function
arg1, arg2 - Is any argument to be used by the function. This
can be represented by a column name or expression.
From tbl_name – is the name of the table
Where:
• LOWER: Converts mixed-case or uppercase character strings to
lowercase
• UPPER: Converts mixed-case or lowercase character strings to
uppercase
• INITCAP: Converts the first letter of each word to uppercase and
the remaining letters to lowercase
Explanation:
It retrieves the record of all authors whose LNAME starts with
capital S then converts the concatenated LNAME and FNAME to
upper case.
Note that since during the insertion of values into authors table all
value is inserted in capital form the reason why no changes in the
conversion as shown in the output.
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
Using Case-Conversion function: lower
Example: lower
SELECT LOWER(LNAME||','||FNAME) AS "NAME"
FROM AUTHORS
WHERE LNAME LIKE'S%';
Output:
Explanation:
It retrieves the record of all authors whose LNAME starts with
capital S then converts the concatenated LNAME and FNAME to
lowercase.
Explanation:
It retrieves the record of all authors whose LNAME starts with
capital S then converts the concatenated LNAME and FNAME to
sentence case like. Where the first letter is capitalized and every
time an SQL encounter special character and or space is
automatically capitalized the first letter of the following string and
or character.
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
Using Character Manipulation Function: Concat
Example: CONCAT
SELECT CONCAT(LNAME,FNAME), YR_PUB
FROM AUTHORS
WHERE YR_PUB = 2010;
Output:
Explanation:
It retrieves the record of all authors whose YR_PUB is equal to
2010, then concatenates the LNAME to FNAME.
Explanation:
It retrieves the record of all authors whose BOOK counting from
1-6 is equal to ING.
Explanation:
It retrieves the record of all authors whose BOOK name starts with
the capital letter A then gets the length of the book as well as the
character position of A in the book column. Notice that the Oracle
only returns the character position of the first A it encounters
starting from the first letter up to the last. Like for example the
work DATABASE is composed of 3 A’s but since it only reads the
first letter A its INSTR value is 2 (second letter equal to A)
Explanation:
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
It retrieves the record of all authors whose BOOK has an ING
string then pads the YR_PUB to the left and replace the other
numbers with $ dollar sign symbol to make the digits size 6, as
well as pads the YR_PUB to the right and replace the other
numbers with $ dollar sign symbol to make the digits size 6.
Explanation:
It retrieves the record of all authors FNAME then replaced the
letter A with T in the column FNAME.
Explanation:
It retrieves the record of all authors BOOK then trim the letter A
from BOOK Column.
Number Function
Function Result
ROUND(75. 46,1) 75.5
TRUNC(75.46,1) 75
MOD(100/3) 10
Where:
• ROUND: a type of number function that is used to rounds value to
a specified decimal
• TRUNC: a type of number function that is used to truncates value
to a specified decimal
• MOD: a type of number function that is used to returns remainder
of division
DUAL Table
The DUAL table is owned by the user SYS and can be accessed by all
users. It contains one column, DUMMY, and one row with the value
X. The DUAL table is useful when you want to return a value only
once (for example, the value of a constant, pseudo column, or
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
expression that is not derived from a table with user data). The DUAL
table is generally used for completeness of the SELECT clause syntax
because both SELECT and FROM clauses are mandatory, and several
calculations do not need to select from the actual tables.
Output:
Explanation:
ROUND(75.475,2) – Round the number starting from decimal
point count to 2 = 75.47 and since the following number of 7 is 5 it
round off to 8 the which results to 75.48
ROUND(75.475,1) – Round the number starting form decimal
point count to 1 = 75.4 and since the following number of 4 is 7 it
round off to 5 the which results to 75.5
ROUND(75.475,-1) – Round the number starting form decimal
point count to 1 to the left(because of – negative sign) = 75 and
since the following number of 7 is 5 it rounds off to 8 and makes
the value of 5 to 0 which results to 80.
Explanation:
TRUNC(75.475,2) – Truncate/cut the number starting from
decimal point 2 which result to 75.47
TRUNC(75.475,1) – Truncate/cut the number starting from
decimal point 1 which result to 75.4
TRUNC(75.475,-1) – Truncate/cut the number starting from the
decimal point -1(starting to the left) which result to 75 and since
we have to cut the digit at position -1 make this digit 0 which
result to 70.
Explanation:
It gets the remainder of 1000 over 300 which is equal to 100.
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
Lesson Summary:
In this lesson, you should have learned how to:
• Perform calculations on data using functions
• Modify case and character values.
Terms to Remember!
• Character functions: Accept character input and can return both
character and number values
• CONCAT: Joins values together (You are limited to using two
parameters with CONCAT.) instead used || (double bars) if it
contains two or more columns and character literal string
• DUAL - the table that is owned by the user SYS and can be
accessed by all users.
• INITCAP: Converts the first letter of each word to uppercase and
the remaining letters to lowercase
• INSTR: Finds the numeric position of a named character
• LENGTH: Shows the length of a string as a numeric value
• LOWER: Converts mixed-case or uppercase character strings to
lowercase
• LPAD: Returns an expression left-padded to the length of n
characters with a character expression
• Multiple-row functions – returns one result per group of row.
• MOD: Returns remainder of division
• Number functions: Accept numeric input and return numeric
values
• ROUND: Rounds value to a specified decimal
• RPAD: Returns an expression right-padded to the length of n
characters with a character expression
• Single-row functions - returns one result per row.
• SUBSTR: Extracts a string of determined length
• TRIM: Trims leading or trailing characters (or both) from a
References Textbook:
• Oracle Press (2010). Applied Oracle Security
References:
• Pratt, Philip J. (2010). Database management systems
• Rob, Peter & Coronel, Carlo (2009). Database Management
Systems
• Schwalbe, Kathy (2011). Management of Information Technology
Projects
• Wheeler, Evan (2011). Security Risk Management : Building an
Information Security Risk Management Program from the Ground
Up
Supplementary Video
https://www.youtube.com/watch?v=5rx8Q4x4-qI
https://www.youtube.com/watch?v=WQe-p2F3Kcg
https://www.youtube.com/watch?v=yjXXntX0sh8
Suggested Reading
• SQL Tutorial. In ws3schools, Retrieved from
_____________________________________________________________________________________
14
MODULE OF INSTRUCTION
http://www.w3schools.com/sql/default.asp
• Database management system. In Encyclopedia Britannica,
Retrieved from
http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
• SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
• Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.aspx
• SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
• Tutorialspoint.com
• oracle.com
• apex.oracle.com