Single row functions

749 views 42 slides Sep 14, 2021
Slide 1
Slide 1 of 42
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42

About This Presentation

Using single row functions


Slide Content

Single-Row Functions

SQL Functions
Function
Input
arg 1
arg 2
arg n
Function
performs action
Output
Result
value

Two Types of SQL Functions
Functions
Single-row
functions
Multiple-row
functions

Single-Row Functions
Single row functions:
Manipulate data items
Accept arguments and return one value
Act on each row returned
Return one result per row
May modify the data type
Can be nested
Accept arguments which can be a column or an
expression
function_name[(arg1, arg2,...)]

Single-Row Functions
Conversion
Character
Number
Date
General
Single-row
functions

Character Functions
Character
functions
LOWER
UPPER
INITCAP
CONCAT
SUBSTR
LENGTH
INSTR
LPAD | RPAD
TRIM
REPLACE
Case-manipulation
functions
Character-manipulation
functions

Function Result
Case Manipulation Functions
These functions convert case for character strings.
LOWER('SQL Course')
UPPER('SQL Course')
INITCAP('SQL Course')
sql course
SQL COURSE
Sql Course

Using Case Manipulation Functions
Display the employee number, name, and
department number for employee Smith:
SELECT empno, ename, deptno
FROM emp
WHERE ename= 'smith';
no rows selected
SELECT empno, ename, deptno
FROM emp
WHERE LOWER(ename) = 'smith';

CONCAT('Hello', 'World')
SUBSTR('HelloWorld',1,5)
LENGTH('HelloWorld')
INSTR('HelloWorld', 'W')
LPAD(salary,10,'*')
RPAD(salary, 10, '*')
TRIM('H' FROM 'HelloWorld')
HelloWorld
Hello
10
6
*****24000
24000*****
elloWorld
Function Result
Character-Manipulation Functions
These functions manipulate character strings:

SELECT ename, CONCAT (ename, job), LENGTH (ename),
INSTR(ename, 'A')
FROM emp
WHERE SUBSTR(job,1,5) = 'SALES'
Using the Character-Manipulation Functions
1
2
31 2
3

Single-Row Functions
Conversion
Character
Number
Date
General
Single-row
functions

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

The Dual Table
TheDUALtable is owned by user SYS and can be accessed by
all users. It contains one column, DUMMY,and one row with
the value X.The DUALtable is useful when you want to
return a value once only –for instance, the value of a
constant, pseudo column, or expression that is not derived
from a table with user data. The DUAL table is generally used
for SELECT clause syntax completeness, because both
SELECT and FROM clauses are mandatory, and several
calculations do not need to select from actual tables.

SELECT ROUND(45.923,2), ROUND(45.923,0),
ROUND(45.923,-1)
FROM DUAL;
Using the ROUNDFunction
DUALis a dummy table you can use to view results
from functions and calculations.
1 2
3
31 2

SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-2)
FROM DUAL;
Using the TRUNCFunction
31 2
1 2
3

SELECT ename, sal, MOD(sal, comm)
FROM emp
WHERE job = 'SALESMAN';
Using the MODFunction
Calculate the remainder of the ratio od salary to
commission for all employees whose job title is
salesman.

Single-Row Functions
Conversion
Character
Number
Date
General
Single-row
functions


Working with Dates
Oracle database stores dates in an internal
numeric format: century, year, month, day,
hours, minutes, seconds.
The default date display format is DD-MON-
YY.
SELECT ename, hiredate
FROM emp
WHERE enamelike ‘S%';

Working with Dates
SYSDATEis a function that returns:
Date / Time

Arithmetic with Dates
Add or subtract a number to or from a date
for a resultant date value.
Subtract two dates to find the number of days
between those dates.

Using Arithmetic Operators
with Dates
SELECT ename, (SYSDATE -hiredate)/7 AS WEEKS
FROM emp
WHERE deptno = 30;

Date Functions
Number of months
between two dates
MONTHS_BETWEEN
ADD_MONTHS
NEXT_DAY
LAST_DAY
ROUND
TRUNC
Add calendar months to
date
Next day of the date
specified
Last day of the month
Round date
Truncate date
Function Description

•MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
Using Date Functions
•ADD_MONTHS ('11-JAN-94',6)
•NEXT_DAY ('01-SEP-95','FRIDAY')
•LAST_DAY('01-FEB-95')
19.6774194
'11-JUL-94'
'08-SEP-95'
'28-FEB-95'

•ROUND(SYSDATE,'MONTH') 01 -AUG-95
•ROUND(SYSDATE ,'YEAR') 01 -JAN-96
•TRUNC(SYSDATE ,'MONTH') 01 -JUL-95
•TRUNC(SYSDATE ,'YEAR') 01 -JAN-95
Using Date Functions
Assume SYSDATE = '25-JUL-95':

Single-Row Functions
Conversion
Character
Number
Date
General
Single-row
functions



Conversion Functions
Implicit data type
conversion
Explicit data type
conversion
Data type
conversion

Explicit Data Type Conversion
NUMBER CHARACTER
TO_CHAR
TO_NUMBER
DATE
TO_CHAR
TO_DATE

Using the TO_CHARFunction with Dates
The format model:
Must be enclosed in single quotation marks and is
case sensitive
Can include any valid date format element
Has an fmelement to remove padded blanks or
suppress leading zeros
Is separated from the date value by a comma
TO_CHAR(date, 'format_model')

Elements of the Date Format Model

Elements of the Date Format Model

Using the TO_CHARFunction with Dates
SELECT ename,
TO_CHAR(hiredate, 'fmDD Month YYYY')
AS "HIRE DATE"
FROM emp

Using the TO_CHARFunction with Numbers
These are some of the format elements you can use with the
TO_CHARfunction to display a number value as a character:
TO_CHAR(number, 'format_model')

SELECT TO_CHAR(sal, '$99,999.00') SALARY
FROM emp
WHERE ename = ‘KING';
Using the TO_CHARFunction with Numbers

Single-Row Functions
Conversion
Character
Number
Date
General
Single-row
functions





Nesting Functions
Single-row functions can be nested to any level.
Nested functions are evaluated from deepest
level to the least deep level.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3

SELECT ename,
NVL(TO_CHAR(mgr), 'No Manager')
FROM emp
WHERE mgr IS NULL;
Nesting Functions

General Functions
These functions work with any data type and pertain
to using nulls.
NVL (expr1, expr2)

NVLFunction
Converts a null to an actual value.
Data types that can be used are date, character,
and number.
Data types must match:
NVL(comm,0)
NVL(hiredate,'01-JAN-97')
NVL(job, 'No Job Yet')

SELECT ename, sal, NVL(comm, 0),
(sal*12) + (sal*12*NVL(comm, 0)) AN_SAL
FROM emp;
Using the NVLFunction

1
2
1 2

Conditional Expressions
Provide the use of IF-THEN-ELSE logic within a
SQL statement
Use two methods:
DECODEfunction

The DECODEFunction
Facilitates conditional inquiries by doing the
work of an IF-THEN-ELSE statement:
DECODE(col|expression, search1, result1
[, search2, result2,..., ]
[, default])

Using the DECODEFunction
SELECT ename, job, sal,
DECODE(job, 'ANALYST', sal*1.1,
'CLERK', sal*1.15,
'MANAGER', sal*1.20,
sal)
REVISED_SALARY
FROM emp;