Chapter 5

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Chapter 5

Single-Row Function(1)

Objectives
Understand

the usage of functions in


SELECT statements
Using Number and Text functions

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.
F
T
G
A
R

Sorting function

A
F
G
R
T

Two Types of SQL Functions


Functions

Single-row
functions

Multiple-row
functions

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

Single-Row Functions
Single-row
functions

Number

Text

This chapter

Date

Conversion

Chapter 6

Calling a Function in SQL


function_name (column|expression, [arg1, arg2,...])

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

Number Functions
Function name

Description

Round (column/expression, Round to a specified


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

Using 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]
(a) Round Function
FROM test;
Output:
Round to 0

Round to 1
5

5.3

Round to 2
5.26

Using Mod Function


It will return the remainder of a division.

SELECT 10 mod 3 As [Mod]


FROM test;
Output:
Mod
1

Text Functions
Text
functions

Case conversion
functions

String manipulation
functions

UCase
LCase

Mid
Trim

StrConv

Len
Instr
Left / Right

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.

Using UCase & LCase


SELECT Ucase('smith') AS UpperCase,
Lcase('SMITH') As LowerCase
FROM test;
Output:
UpperCase
SMITH

LowerCase
smith

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
MY NAME

LowerCase
my name

Initial
My Name

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

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.

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

MID Function
Mid (stringexpression, start, length)
Stringexpression
The

field or the value that will be used 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.

Example on MID function


SELECT Mid(CourseDesp,12,8) As [Middle of String],
Mid(CourseDesp,12) As [Rest of String]
FROM course;
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

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 used for searching.

Search_string
The

value or the pattern that you are looking for in the


stringexpression

Example on INSTR Function


SELECT CourseDesp, Instr(1,CourseDesp,'C')
As [Position of C]
FROM course;
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

LEFT / RIGHT Function


Left (stringexpression, n)
Or

Right (stringexpression, n)
Stringexpression
The

field or the value that will be used for


manipulation.

n
The

number of characters that will be


returned from the stringexpression.

Example on LEFT/RIGHT function


SELECT MentorName, Left(MentorName,3) As
[Left], Right(MentorName,3) As [Right]
FROM Mentor;
Output
Left

MentorName

Right

Goile

Goi

ile

Rimes

Rim

mes

Christopher

Chr

her

Schubert

Sch

ert

Norman

Nor

man

Carroll

Car

oll

TRIM Function
Trim (stringexpression)
Stringexpression
The

field or the value that spaces need to be


removed

Example on TRIM function


SELECT TRIM(

Hello

Output:
Greetings
Hello

) As [Greetings] .

LEN Function
Len (stringexpression)
Stringexpression
The

field or the value that the length to be


calculate.

Example on LEN Function


SELECT MentorName, Len(MentorName) As [Length]
FROM Mentor;
Output:
MentorName

Length

Goile

Rimes

Christopher

11

Schubert

Norman

Carroll

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

Summary
Must

know the features of Single-Row


functions.
Usage of both Text and Number
functions.

You might also like