0% found this document useful (0 votes)
38 views

Lecture05 SingleRowFunctions Part1

This document discusses single-row functions in SQL. It covers character functions like LOWER, UPPER, CONCAT, and SUBSTR. Number functions like ROUND and TRUNC are described. Date functions such as MONTHS_BETWEEN, ADD_MONTHS, and NEXT_DAY are also summarized. Examples are provided to demonstrate how to use these functions to manipulate data and perform calculations on character strings, numbers, and dates.

Uploaded by

Arman Anwar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lecture05 SingleRowFunctions Part1

This document discusses single-row functions in SQL. It covers character functions like LOWER, UPPER, CONCAT, and SUBSTR. Number functions like ROUND and TRUNC are described. Date functions such as MONTHS_BETWEEN, ADD_MONTHS, and NEXT_DAY are also summarized. Examples are provided to demonstrate how to use these functions to manipulate data and perform calculations on character strings, numbers, and dates.

Uploaded by

Arman Anwar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1

Introduction to Database
Lecture 05:
Single-Row Functions (Part 1)
Learning Objectives
2

To know about:
SQL Functions
Types of SQL Functions
Single-Row Functions
 Character Functions
 Number Functions

 Date Functions
SQL Functions
3

Input Output
Function

arg 1 Function
performs action
arg 2
Result
value

arg n
Two Types of SQL Functions
4

Functions

Single-row Multiple-row
functions functions
Single-Row Functions
5
• Manipulate data items
• Accept arguments and return one value
• Act on each row returned
• Return one result per row
• May modify the datatype
• Can be nested

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


Single-Row Functions
6

Character

General Number
Single-row
functions

Conversion Date
Character Functions
7

Character
functions

Case conversion Character manipulation


functions functions
CONCAT
LOWER Trim
SUBSTR Ltrim, Rtrim
UPPER LENGTH Replace
INSTR
INITCAP
LPAD, RPAD
Case Conversion Functions
8

Convert case for character strings


Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
Using Case Conversion Functions
9

Display the employee number, name, and


department number for employee Blake.
SQL> SELECT empno, ename, deptno
2 FROM emp
3 WHERE ename = 'blake';
no rows selected

SQL> SELECT empno, ename, deptno


2 FROM emp
3 WHERE LOWER(ename) = 'blake';

EMPNO ENAME DEPTNO


--------- ---------- ---------
7698 BLAKE 30
Character Manipulation Functions
10

Manipulate character strings


Function Result
CONCAT('Good', 'String') GoodString
SUBSTR('String',1,3) Str
LENGTH('String') 6
INSTR('String', 'r') 3
Trim(‘S’ from ‘SSMITH’) MITH
Replace(‘toy’,’y’,’let’) tolet
Using the Character Manipulation Functions
11

SQL> SELECT ename, CONCAT (ename, job), LENGTH(ename),


2 INSTR(ename, 'A')
3 FROM emp
4 WHERE SUBSTR(job,1,5) = 'SALES';

ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A')


---------- ------------------- ------------- ----------------
MARTIN MARTINSALESMAN 6 2
ALLEN ALLENSALESMAN 5 1
TURNER TURNERSALESMAN 6 0
WARD WARDSALESMAN 4 2
Number Functions
12
 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
Using the ROUND Function
13

SQL> SELECT ROUND(45.923,2), ROUND(45.923,0),


2 ROUND(45.923,-1)
3 FROM DUAL;

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


--------------- -------------- -----------------
45.92 46 50
Using the TRUNC Function
14

SQL> SELECT TRUNC(45.923,2), TRUNC(45.923),


2 TRUNC(45.923,-1)
3 FROM DUAL;

TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1)


--------------- ------------- ---------------
45.92 45 40
Using the MOD Function
15

Calculate the remainder of the ratio of salary to


commission for all employees whose job title is
salesman.

SQL> SELECT ename, sal, comm, MOD(sal, comm)


2 FROM emp
3 WHERE job = 'SALESMAN';

ENAME SAL COMM MOD(SAL,COMM)


---------- --------- --------- -------------
MARTIN 1250 1400 1250
ALLEN 1600 300 100
TURNER 1500 0 1500
WARD 1250 500 250
Working with Dates
16

• Oracle stores dates in an internal numeric format:


century, year, month, day, hours, minutes, seconds.
• The default date format is DD-MON-YY.
• SYSDATE is a function returning date and time.
• DUAL is a dummy table used to view SYSDATE.
Arithmetic with Dates
17

• 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.
• Add hours to a date by dividing the number of hours
by 24.
Using Arithmetic Operators with Dates
18

SQL> SELECT ename, (SYSDATE-hiredate)/7 WEEKS


2 FROM emp
3 WHERE deptno = 10;

ENAME WEEKS
---------- ---------
KING 830.93709
CLARK 853.93709
MILLER 821.36566
Date Functions
19

Function Description

MONTHS_BETWEEN Number of months


between two dates
ADD_MONTHS Add calendar months to
date
NEXT_DAY Next day of the date
specified

LAST_DAY Last day of the month


ROUND Round date

TRUNC Truncate date


Using Date Functions
20

• MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
19.6774194

• ADD_MONTHS ('11-JAN-94',6) '11-JUL-94'

• NEXT_DAY ('01-SEP-95','FRIDAY') '08-SEP-95'

• LAST_DAY('01-SEP-95') '30-SEP-95'
Using Date Functions
21

Round(to_date(’25-jul-05’),’month’) 01-Aug-05
Round(to_date(’25-jul-05’),’Year’) 01-Jan-06

Trunc(to_date(’25-jul-05’),’month’) 01-Jul-05

Trunc(to_date(’25-jul-05’),’year’) 01-Jan-05
22

THANK YOU

You might also like