You are on page 1of 21

CHAPTER IV

SINGLE ROW FUNCTION

OBJECTIVES
After completing this lesson, you should be able
to do the following:
1.Describes various types of functions available in
SQL.
2.Use character, number, and date functions in
SELECT statements.
3.Describe the use of conversion functions.

SQL FUNCTIONS
Functions are a very powerful feature of SQL
and can be used to do the following:
1.Perform calculations on data
2.Modify individual data items
3.Manipulate output for groups of rows
4.Formats dates and numbers for display
5.Convert column data types

TWO TYPE OF SQL FUNCTIONS


1. Single row function these functions operate
on single rows only and return one result per
row.
2. Multiple row function these functions can
manipulate groups of rows to give one result per
group of rows.

SINGLE ROW FUNCTION


Manipulate data items
Accepts arguments and return one value
Act on each row returned
Return one result per row
May modify the data type
Can be nested
Accepts arguments which can be column or an
expression.
Syntax:
Function_name [(arg1, arg2,)]

CHARACTER FUNCTIONS
Case
manipulation

Character
manipulation

LOWER

CONCAT

UPPER

SUBSTR

INITCAP

LENGTH
INSTR
LPAD/RPAD
TRIM

CHARACTER FUNCTIONS: MEANING


1.
2.
3.

Lower covert alpha character values to lowercase.


Upper convert alpha character values to uppercase.
Initcap convert alpha character values to uppercase for
the first letter of each word.
4. Concat concatenates the first character value to the
second character value.
5. Substr returns specified character from character value
starting at character position n.
6. Length returns the number of character in the expression.
7. Instr returns the numeric position of a named string.
8. Lpad pads the character value right justified.
9. Rpad pads the character value left justified.
10. Trim trim heading or trailing characters (or both) from
character string.
11. Replace searhes a expression for a character string and if
found, replaces it with a specified replacement string.

CASE MANIPULATION FUNCTIONS


These functions convert case for character
strings.
Function

Result

LOWER ( SQL Course )

sql course

UPPER ( SQL Course )

SQL
COURSE

INITCAP ( SQL Course ) Sql Course

USING CASE MANIPULATION


FUNCTIONS
Display the employee number, name, and
department number for employee matos:
Example:
select upper(last_name)
from employees
where last_name = 'matos';
upper(last_name)
MATOS

CHARACTER MANIPULATION
FUNTIONS
Function

Result

CONCAT(Hello, World)

HelloWorld

SUBSTR(HelloWorld, 1,5)

Hello

LENGTH (HelloWorld)

10

INSTR(HelloWorld,W)

LPAD(salary,10,*)

*****24000

RPAD(salary,10,*)

24000*****

USING THE CHARACTER


MANIPULATION FUNCTIONS
select employee_id,
concat(last_name,first_name)Name
from employees;
employee_id

10 rows selected

Name
100kingsteven
101kocharnena
102de haanlex
103hunoldalexander
104ernstbruce
107Lorentzdiana

USING THE CHARACTER


MANIPULATION FUNCTIONS
select employee_id,
concat(last_name,first_name)Name,
length(last_name),instr(last_name,'a')
from employees;
employee_i
length(last_na instr(last_name
d
Name
me)
,'a')
100kingsteven
4
0
101kocharnena
6
5
102de haanlex
7
5
hunoldalexan
103 der

104ernstbruce
lorentzdian

6
5

0
0

USING THE CHARACTER


MANIPULATION FUNCTIONS
select employee_id,
concat(last_name,first_name)Name,job_id,
length(last_name),instr(last_name,'a')
from employees
where substr(job_id, 4) = 'clerk';
employee_
id

Name

mourgoske
124 vin

job_id

length(last_nam instr(last_name,
e)
'a')

st_clerk
7
141rajstrina st_clerk
4
daviascu
Note: SUBSTR search for the employees who have the string
rtis in the job_id
st_clerk
6
clerck142
contained
stating at fourth position.

0
2
2

USING THE CHARACTER


MANIPULATION FUNCTIONS
select concat(last_name,first_name)Name,
job_id,lpad(salary,4,'@'), rpad(salary,2,'$')
from employees;
Name
job_id
kingsteven ad_pres
kocharnena ad_vp
de haanlex ad_vp
hunoldalexan
der

it_prog
ernstbruce it_prog
lorentzdian
a
st_man

lpad(salary,4,' rpad(salary,2,'$
@')
')

2400
1700
1700

24
17
17

9000
6000

90
60

4200

42

NUMBER FUNCTIONS
ROUND: rounds value to specified decimal
ROUND (45.926, 2) = 45.93

TRUNC: truncates value to specified decimal


TRUNC (45.926, 2) = 45.92

MOD: returns remainder of division


MOD (1600, 300) = 100

NUMBER FUNCTIONS
Number functions accept numeric input and
return numeric value.
Function

Purpose

Round (column |
expression, n)

Round , the column, expression, or


value to n decimal place, or, if n is
omitted no decimal places. (if in is
negative, numbers to left of the
decimal point are rounded)
Truncates the column, expression, or
value to n decimal places, or, if n is
omitted, then n defaults to zero.

trunc (column |
expression, n)
Mod(m,n)

Returns the remainder of m divided


by n.

THE DUAL TABLE


The dual table is owned by the user SYS and can
be accessed by all users.
DUAL table is useful when you want to return a
value once only.
The dual table is generally used for SELECT
clause syntax completeness, because both select
and from clause are mandatory, and several
calculations do not need to select from actual
table.
select column_list
From dual;

USING THE ROUND FUNCTION


SELECT

FROM

ROUND(45.923,2),ROUND(45.923,0),
ROUND(45.923, -1), ROUND(45.923)

DUAL;

ROUND(45.923, ROUND(45.923, ROUND(45.923,


2)
0)
-1)
ROUND(45.923)

45.92

46

50

46

EXAMPLE OF ROUND FUNCTION


Round off the salary of employees whose job title
is st_clerk.
SELECT
from
where

round(salary),round(salary),
round(salary,-1)
employees
job_id = 'st_clerk';

round(salary)

round(salary)

round(salary,-1)

58000

58000

58000

58000

58000

58000

58000

58000

58000

USING THE TRUNC FUNCTION


SELECT
FROM

TRUNCATE(45.923,2),
TRUNCATE(45.923,0),
TRUNCATE(45.923,-1)
DUAL;

TRUNCATE(45.923, TRUNCATE(45.923, TRUNCATE(45.923,


2)
0)
-1)

45.92

45

40

USING THE MOD FUNCTION


SELECT
FROM

MOD(1600,300)
DUAL;

MOD(1600,300)
100

You might also like