You are on page 1of 15

MODULE OF INSTRUCTION

Formatting Case, Character and


Number using Single-row Function
Welcome to the 8th module of this course Database Management
System 1! For this lesson, it covers the different conditions that you
can apply in order to change and get the character or case value in the
table. Sample and demonstration in this lesson uses the AUTHORS
table as shown below:
Table 1.0 Authors

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

Database Management System 1 1


Week 3 Single Row Function

Functions are a very powerful feature of SQL. They can be used to do


the following:
• Perform calculations on data
• Modify individual data items
• Manipulate output for groups of rows
• Format dates and numbers for display
• Convert column data types

There are two types of functions:


1. Single-row functions - returns one result per row.
These functions operate on single rows only and return one result
per row. There are different types of single-row functions. This
lesson covers the following functions:
• Character
• Number
2. Multiple-row functions – returns one result per group of row.
Functions can manipulate groups of rows to give one result per
group of rows. These functions are also known as group functions.
This will be discuss in Week 11 lesson.

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

Two types of Single row function


1. Character functions: is a type of single row function that may
accept character input and can return both character and number
values
Character functions can be divided into the following:
1. Case-conversion functions
2. Character-manipulation functions
2. Number functions: a type of single row function that may accept
numeric input and return numeric values
Number functions can be divided into the following:
1. ROUND
2. TRUNC
3. MOD

Case Conversion function:


Function Result
LOWER(‘Database database management system
Management System’)
UPPER(‘Database Management DATABASE
System’) MANAGEMENT SYSTEM
INITCAP(‘Database Database Management System
Management System’)

Database Management System 1 3


Week 3 Single Row Function

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

Using Case-Conversion function: UPPER


 Example: UPPER
SELECT UPPER(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
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.

Using Case-Conversion function: InitCap


 Example: lower
SELECT INITCAP(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
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

Database Management System 1 5


Week 3 Single Row Function

or character.

Character Manipulation Function


Function Result
CONCAT(‘Data’,’Base’) DataBase
SUBSTR(Database System, 1, 8) Database
LENGTH(‘Database’) 8
INSTR(Database,’b’) 5
LPAD(price,6,’$’) $$$360
RPAD(proce,6,’$’) 360$$$
REPLACE(Database, ‘a’,’c’) Dctcbce
TRIM(‘D’ from ‘Database’) atabase
Where:
• 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
• SUBSTR: Extracts a string of determined length
• LENGTH: Shows the length of a string as a numeric value
• INSTR: Finds the numeric position of a named character
• LPAD: Returns an expression left-padded to the length of n
characters with a character expression
• RPAD: Returns an expression right-padded to the length of n
characters with a character expression
• TRIM: is sued to trim the leading or trailing characters (or both)
from a character string (If trim_character or trim_source is a
character literal, you must enclose it in single quotation marks.)

_____________________________________________________________________________________
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.

Character Manipulation Function: SUBSTR


 Example: SUBSTR
SELECT LNAME, FNAME, BOOK
FROM AUTHORS
WHERE SUBSTR(BOOK,6)='ING';
 Output:

 Explanation:
It retrieves the record of all authors whose BOOK counting from
1-6 is equal to ING.

Database Management System 1 7


Week 3 Single Row Function

Character Manipulation Function: INSTR and LENGTH


 Example: LENGTH, INSTR
SELECT BOOK,LENGTH(BOOK), INSTR(BOOK,'A')
FROM AUTHORS
WHERE BOOK LIKE 'A%';
 Output:

 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)

Character Manipulation Function: LPAD and RPAD


 Example: RPAD, LPAD
SELECT BOOK, LPAD(YR_PUB,7,'$'),
RPAD(YR_PUB,7,'@')
FROM AUTHORS
WHERE BOOK LIKE '%ING%';
 Output:

 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.

Character Manipulation Function: Replace


 Example: REPLACE
SELECT REPLACE(FNAME,'A','T')
FROM AUTHORS;
 Output:

 Explanation:
It retrieves the record of all authors FNAME then replaced the
letter A with T in the column FNAME.

Character Manipulation Function: Trim


 Example: TRIM
SELECT BOOK, TRIM('A' FROM BOOK)
FROM AUTHORS;
 Output:

Database Management System 1 9


Week 3 Single Row Function

 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.

Using Number Function: ROUND


 Example: ROUND
SELECT ROUND(75.475,2),ROUND(75.475,1),
ROUND(75.475,-1)
FROM DUAL;

 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.

Database Management System 1 11


Week 3 Single Row Function

Using Number Function: TRUNC


 Example: TRUNC
SELECT
TRUNC(75.475,2),TRUNC(75.475,1),TRUNC(75.475,-1)
FROM DUAL;
 Output:

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.

Using Number Function: MOD


 Example: MOD
SELECT MOD(1000,300)
FROM DUAL;
 Output:

 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

Database Management System 1 13


Week 3 Single Row Function

character string (If trim_character or trim_source is a character


literal, you must enclose it within single quotation marks.)
• TRUNC: Truncates value to a specified decimal
• UPPER: Converts mixed-case or lowercase character strings to
uppercase

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 Reading and Video


Supplementary Reading
https://docs.oracle.com/database/121/SQLRF/functions002.htm#SQLRF511
78
https://www.tutorialspoint.com/sql_certificate/using_single_row_functions
.htm
http://database-query.blogspot.com/2007/10/basic-select-
statement_18.html

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

Database Management System 1 15

You might also like