3. What is Function?
A function is a type of procedure or routine
that performs a specific task.
A function can receive argument and
always returns a value.
Sorting function
F
T
G
A
R
A
F
G
R
T
4. Two Types of SQL Functions
Functions
Single-row
functions
Multiple-row
functions
5. Single-Row Functions
Manipulate data items
Accept arguments and return one value
Act on each row returned
Return one result per row
Can modify the data type
Can be nested
7. Calling a Function in SQL
function_name Is the name of the
function
column Is any named
database column
expression Is any character
string or calculated
expression
arg1, arg2 Is any argument to be
used by the function
function_name (column|expression, [arg1, arg2,...])
8. Number Functions
Function name Description
Round (column/expression,
precision)
Round to a specified
precision
Mod
Example: ( x mod y)
Remainder after
division
Note: There are lots of other Number functions but the one highlights in the course
is more useful
9. Using ROUND Function
(a) Round Function
SELECT Round(5.255,0) as [Round to 0],
Round(5.255,1) as [Round to 1],
Round(5.255,2) as [Round to 2]
FROM test;
Output:
Round to 0 Round to 1 Round to 2
5 5.3 5.26
10. Using Mod Function
It will return the remainder of a division.
SELECT 10 mod 3 As [Mod]
FROM test;
Output:
Mod
1
12. Case Conversion Functions
Functions name Description
UCase (column/expression)
or
StrConv(column/expression, 1)
Puts all the letters in uppercase
LCase (column/expression)
or
StrConv(column/expression, 2)
Puts all the letters in lower case
StrConv(column/expression, 3) Capitalizes the first letter of each
word and puts all the other letters
in lowercase.
13. Using UCase & LCase
SELECT Ucase('smith') AS UpperCase,
Lcase('SMITH') As LowerCase
FROM test;
Output:
UpperCase LowerCase
SMITH smith
14. Using StrConv
SELECT StrConv('my name',1) as [UpperCase],
StrConv('my name',2) as [LowerCase],
StrConv('my name',3) as [Initial]
FROM test;
Output:
UpperCase LowerCase Initial
MY NAME my name My Name
15. Example of Case conversion
SELECT Ucase (CourseDesp) as [Course Name]
FROM Course;
Output:
Course Name
DIPLOMA IN COMPUTER STUDIES
DIPLOMA IN GAMING AND ANIMATION TECHNIQUES
DIPLOMA IN COMPUTING
DIPLOMA IN INFO-COMM TECHNOLOGY
DIPLOMA IN INFORMATION TECHNOLOGY
DIPLOMA IN NETWORK AND CYBERSECURITY
16. String Manipulation Functions
Perform operations of string such as
creating a substring
Extract a portion of the string to create a
new character string output
These functions are useful when you want
to lookup certain data /values from a
particular string.
17. String Manipulation Functions
Functions Name Description
Mid (stringexpression, start, length) Returns part of a string.
Instr(start, stringexpression, search_string) Finds the occurrence of
some search-string pattern
Left(stringexpression, n)
Or
Right(stringexpression, n)
Returns a portion of a
string starting either left or
right.
Trim(stringexpression) Removes blanks spaces
from both sides of a string
Len(stringexpression) Returns the length of a
string
18. MID Function
Mid (stringexpression, start, length)
Stringexpression
The field or the value that will be use for the function
to manipulate.
Start
Tells Access where in the stringexpression to start
retrieving from
Length
The number of characters to extract. If the length is
absent, then function will returns the rest of the string
from where you start.
19. Example on MID function
Output:
Middle of String Rest of String
Computer Computer Studies
Gaming a Gaming and Animation Techniques
Computin Computing
Info-Com Info-Comm Technology
Informat Information Technology
Network Network and CyberSecurity
SELECT Mid(CourseDesp,12,8) As [Middle of String],
Mid(CourseDesp,12) As [Rest of String]
FROM course;
20. INSTR Function
Instr (start, stringexpression, search_string)
Start
Specify the position from which Access will start
reading the stringexpression.
Stringexpression
The field or the value that will be use for searching.
Search_string
The value or the pattern that you looking for in the
stringexpression
21. Example on INSTR Function
Output:
CourseDesp Position of C
Diploma In Computer Studies 12
Diploma in Gaming and Animation
Techniques
35
Diploma In Computing 12
Diploma In Info-Comm Technology 17
Diploma In Information Technology 26
Diploma in Network and CyberSecurity 24
SELECT CourseDesp, Instr(1,CourseDesp,'C')
As [Position of C]
FROM course;
22. LEFT / RIGHT Function
Left (stringexpression, n)
Or
Right (stringexpression, n)
Stringexpression
The field or the value that will be use for
manipulate.
n
The number of characters that will be
returned from the stringexpression.
23. Example on LEFT/RIGHT function
SELECT MentorName, Left(MentorName,3) As
[Left], Right(MentorName,3) As [Right]
FROM Mentor;
Output
MentorName
Left Right
Goile Goi ile
Rimes Rim mes
Christopher Chr her
Schubert Sch ert
Norman Nor man
Carroll Car oll
27. Example on LEN Function
SELECT MentorName, Len(MentorName) As [Length]
FROM Mentor;
Output:
MentorName
Length
Goile 5
Rimes 5
Christopher 11
Schubert 8
Norman 6
Carroll 7
28. Using String Functions
SELECT Mid(FirstName,1,Instr(1,FirstName,' ')) & ', ' &
left(lastname,1) AS [Name]
FROM Student;
Output:
Name
Joseph , B
Laura , K
Choy , L
Derrick , L
Arthur , L
Kathleen , M
………
29. Summary
Must know the features of Single-Row
functions.
Usage of both Text and Number
functions.
Notas del editor
Character Functions
Single-row character functions accept character data as input and can return both character and number values. Character functions can be divided into:
Case conversion functions: Convert the case of character strings
Character manipulation functions: Perform operations of strings such as creating a substring
This lesson covers the case conversion functions. Character manipulation functions are not covered in this course.
Note: This list is a subset of the available character functions.
For more information, see Oracle8 Server SQL Reference, “Character Functions.”