SlideShare a Scribd company logo
SQL Fundamentals Oracle 11g
M U H A M M A D WA H E E D
O R AC L E DATA BA S E D E V E LO P E R
E M A I L : m .wa h e e d 3 6 6 8 @ g m a i l . co m
Lecture#6
SQL Functions
Function Structure
2
SQL Functions(cont’d)
Functions are powerful feature of SQL and can be used to do the
following:
•Perform calculations on data
•Modify individual data items
•Manipulate output for group of rows
•Format dates and numbers for display
•Convert column data type
3
SQL Functions(cont’d)
There are following two types of SQL functions:
•Single Row Functions
Operate on single rows only and return single result per row.
•Multiple Row Functions
Can be used to manipulate group of rows to generate single result
against group of rows and also known as “Group Functions”.
*For now, We will consider only single-row functions.
4
Single-Row Functions
There are following types of single-row functions:
5
Single-Row(cont’d)
There are following types of character function:
6
Character Functions
There are following case-manipulation functions:
•LOWER(<column_name>|<expression>)
converts alpha characters to lowercase
•UPPER(<column_name>|<expression>)
converts alpha characters to uppercase
•INITCAP(<column_name>|<expression>)
capitalize the first letter of each word
7
Character Functions(cont’d)
•CONCAT(<column_name1>|<expression1>,
(<column_name2>|<expression2>))
equivalent to concatination operator
•SUBSTR(<column_name>|<expression>,M[,N])
returns characters starting from m and ending at n. As n is optional
if it is skipped then by default characters are returned from m to end
of characters.
•LENGTH(<column_name>|<expression>)
returns total no. of characters
8
Character Functions(cont’d)
9
Character Functions(cont’d)
10
Use of Character Functions
11
•Example:
SELECT std_id, CONCAT(std_first_name,std_last_name) AS
std_name, LENGTH(std_address), INSTR( std_address, ‘#’)
FROM student
WHERE SUBSTR(std_name,2,3) = ‘ai’;
Number Functions
12
•Number functions input as well as output only numeric values.
Number Functions(cont’d)
13
•Number functions input as well as output only numeric values.
Number Functions(cont’d)
14
•It can have negative value of ‘n’.
•Example:
SELECT ROUND(45.926,-1) FROM DUAL;
Result: 50
SELECT TRUNC(45.926,-2) FROM DUAL;
Result: 0
Date Functions
15
•For current date we use:
SELECT SYSDATE FROM DUAL;
Result( standard/default format): 26-APR-18
Elements of Date Functions
16
Date Functions(cont’d)
17
Arithmetic - Date Functions(cont’d)
18
Use Date Functions(cont’d)
19
•Example:
SELECT std_id, (SYSDATE - admission_date)/ 7 AS weeks
FROM student;
Use Date Functions(cont’d)
20
Implicit type casting
21
•For assignments, Oracle server can automatically convert following:
Implicit type casting(cont’d)
22
•For expression, Oracle server can automatically convert following:
Explicit type casting
23
Explicit type casting(cont’d)
24
Type casting - Date Functions(cont’d)
25
•Example:
i- SELECT std_id, TO_CHAR(admission_date,’MM/YY’)
FROM student;
ii- SELECT std_id, TO_CHAR(admission_date,’Day Month DD,YYYY’)
AS “Admission Date”
FROM student;
iii- SELECT std_id, TO_CHAR(admission_date,’fmDay Month DD,YYYY’)
AS “Admission Date”
FROM student;
Type casting - Date Functions(cont’d)
26
•Example:
i- SELECT std_id, TO_DATE(’27-APR-2018,’MM/YY’)
FROM student;
ii- SELECT std_id, TO_DATE(‘MAY 27,2018’,’fxMonth DD,YYYY’) AS
“Admission Date”
FROM student;
Type casting - Date Functions(cont’d)
27
•Example:
SELECT std_id, TO_CHAR(admission_date,’ fmDD MONTH,YYYY
fmHH12:MI:SS AM’)
FROM student;
•Result(TO_CHAR): 26 APRIL,2018 2:00:00 PM
Motivational Speaking
28
NVL Function
29
•Converts a null value to an actual value
•Syntax:
NVL(<column/experession>,<value>)
•Used with NUMBER,VARCHAR2 and DATE datatypes
•Data types must match for actual value
- NVL(std_age,0)
- NVL(std_address,’Pakistan’)
- NVL(std_dob,’01-JAN-00’)
CASE Function
30
•It provides IF-THEN-ELSE nesting facility in SQL
•Syntax:
CASE Function(cont’d)
31
•Example:
SELECT tch_id,tch_name,tch_salary,
CASE tch_designation WHEN ‘VISITING’ salary*1.1
WHEN ‘ASSISTANT LECTURER’ salary*1.2
WHEN ‘LECTURER’ salary*1.5
ELSE salary END AS “Revised Salary”
FROM teacher;
•*Note: same functionality can be performed using DECODE in
Oracle. Do it by yourself.
Records Manipulation Function
32
•There are few such functions: COUNT,MAX,MIN
•Example:
i- SELECT COUNT(*) FROM student;
ii- SELECT MAX(tch_salary) FROM teacher;
iii- SELECT MIN(std_dob) FROM student;
Nested Single-row Functions
33
•Single functions can be nested to any level.
Nested Single-row Functions
34
•Single functions can be nested to any level.
SELECT NVL(TO_CHAR(std_dob,’MON DD,YYYY’),’NO VALUE’)
FROM student;
Feedback/Suggestions?
The feedback I get is that my books are honest.
- Laurie Halse Anderson

More Related Content

PDF
SQL Subqueries - Oracle SQL Fundamentals
PDF
Basics of SELECT Statement - Oracle SQL
PDF
SQL Functions - Oracle SQL Fundamentals
PDF
SQL Joins - Oracle SQL Fundamentals
PDF
Oracle SQL Fundamentals - Lecture 3
PDF
SQL Readable Outputs - Oracle SQL Fundamentals
PPT
20120810 tsql tips
PDF
Data Manipulation(DML) and Transaction Control (TCL)
SQL Subqueries - Oracle SQL Fundamentals
Basics of SELECT Statement - Oracle SQL
SQL Functions - Oracle SQL Fundamentals
SQL Joins - Oracle SQL Fundamentals
Oracle SQL Fundamentals - Lecture 3
SQL Readable Outputs - Oracle SQL Fundamentals
20120810 tsql tips
Data Manipulation(DML) and Transaction Control (TCL)

What's hot (20)

PDF
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
PDF
SQL Fundamentals - Lecture 2
PPT
PPTX
Oracle: Basic SQL
PPTX
Java Tutorial Lab 8
PDF
Refinement Types for Haskell
PDF
Introduction to oracle functions
PDF
Comparing Haskell & Scala
PPTX
Oracle: Functions
PDF
JavaScript: Variables and Functions
PPT
358 33 powerpoint-slides_6-strings_chapter-6
PPTX
Sql operator
PDF
Scala for Java Developers
PPTX
Queues
PDF
Introduction To Oracle Sql
PPTX
Java Tutorial Lab 7
PPTX
Java Tutorial Lab 5
PPTX
MYSQL single rowfunc-multirowfunc-groupby-having
PPTX
MYSql manage db
PDF
SQL Functions and Operators
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
SQL Fundamentals - Lecture 2
Oracle: Basic SQL
Java Tutorial Lab 8
Refinement Types for Haskell
Introduction to oracle functions
Comparing Haskell & Scala
Oracle: Functions
JavaScript: Variables and Functions
358 33 powerpoint-slides_6-strings_chapter-6
Sql operator
Scala for Java Developers
Queues
Introduction To Oracle Sql
Java Tutorial Lab 7
Java Tutorial Lab 5
MYSQL single rowfunc-multirowfunc-groupby-having
MYSql manage db
SQL Functions and Operators
Ad

Similar to SQL Functions - Oracle SQL Fundamentals (20)

PPTX
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
PPT
Single-Row Functions in orcale Data base
PDF
lab2sql222222222222222222222222222222222
PDF
SQL BUILT-IN FUNCTION
PPT
PDF
Oracle sql functions
PPT
Single row functions
PPT
Built in functions singlerow & group.ppt
PPT
Les03 Single Row Functions in Oracle and SQL.ppt
PPT
PPTX
Les03.pptx
PPTX
Function and types
PPT
Les03[1] Single-Row Functions
PPT
Using single row functions to customize output
PDF
Sql functions
PPT
Single row functions
PPT
Les03 (Using Single Row Functions To Customize Output)
PPT
Oracle sql ppt2
PPT
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Single-Row Functions in orcale Data base
lab2sql222222222222222222222222222222222
SQL BUILT-IN FUNCTION
Oracle sql functions
Single row functions
Built in functions singlerow & group.ppt
Les03 Single Row Functions in Oracle and SQL.ppt
Les03.pptx
Function and types
Les03[1] Single-Row Functions
Using single row functions to customize output
Sql functions
Single row functions
Les03 (Using Single Row Functions To Customize Output)
Oracle sql ppt2
Ad

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
sap open course for s4hana steps from ECC to s4
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx

SQL Functions - Oracle SQL Fundamentals