SlideShare a Scribd company logo
1 of 42
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
3
1 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
 The DUAL table 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 DUAL table 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 ROUND Function
DUAL is a dummy table you can use to view results
from functions and calculations.
1 2
3
3
1 2
SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-2)
FROM DUAL;
Using the TRUNC Function
3
1 2
1 2
3
SELECT ename, sal, MOD(sal, comm)
FROM emp
WHERE job = 'SALESMAN';
Using the MOD Function
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 ename like ‘S%';
Working with Dates
SYSDATE is 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_CHAR Function 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 fm element 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_CHAR Function with Dates
SELECT ename,
TO_CHAR(hiredate, 'fmDD Month YYYY')
AS "HIRE DATE"
FROM emp
Using the TO_CHAR Function with Numbers
These are some of the format elements you can use with the
TO_CHAR function 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_CHAR Function 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)
NVL Function
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 NVL Function
…
1
2
1 2
Conditional Expressions
 Provide the use of IF-THEN-ELSE logic within a
SQL statement
 Use two methods:
 DECODE function
The DECODE Function
Facilitates conditional inquiries by doing the
work of an IF-THEN-ELSE statement:
DECODE(col|expression, search1, result1
[, search2, result2,...,]
[, default])
Using the DECODE Function
SELECT ename, job, sal,
DECODE(job, 'ANALYST', sal*1.1,
'CLERK', sal*1.15,
'MANAGER', sal*1.20,
sal)
REVISED_SALARY
FROM emp;
…

More Related Content

What's hot

Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Achmad Solichin
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testerstlvd
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONArun Sial
 
MYSQL Aggregate Functions
MYSQL Aggregate FunctionsMYSQL Aggregate Functions
MYSQL Aggregate FunctionsLeroy Blair
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and OperatorsMohan Kumar.R
 
oracle Sql constraint
oracle  Sql constraint oracle  Sql constraint
oracle Sql constraint home
 
Database Programming using SQL
Database Programming using SQLDatabase Programming using SQL
Database Programming using SQLAjit Nayak
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 

What's hot (20)

Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Sql operator
Sql operatorSql operator
Sql operator
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
 
MYSQL Aggregate Functions
MYSQL Aggregate FunctionsMYSQL Aggregate Functions
MYSQL Aggregate Functions
 
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
oracle Sql constraint
oracle  Sql constraint oracle  Sql constraint
oracle Sql constraint
 
Database Programming using SQL
Database Programming using SQLDatabase Programming using SQL
Database Programming using SQL
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Pointers
PointersPointers
Pointers
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Python list
Python listPython list
Python list
 
Create table
Create tableCreate table
Create table
 

Similar to Single row functions

Similar to Single row functions (20)

Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
 
Les03
Les03Les03
Les03
 
Les03
Les03Les03
Les03
 
Les03
Les03Les03
Les03
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)
 
Sql 3
Sql 3Sql 3
Sql 3
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03
 
Function and types
Function  and typesFunction  and types
Function and types
 
Single row functions
Single row functionsSingle row functions
Single row functions
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Les03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptLes03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.ppt
 
e computer notes - Single row functions
e computer notes - Single row functionse computer notes - Single row functions
e computer notes - Single row functions
 
Les03
Les03Les03
Les03
 
2 sql - single-row functions
2   sql - single-row functions2   sql - single-row functions
2 sql - single-row functions
 
SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
 
Les03.pptx
Les03.pptxLes03.pptx
Les03.pptx
 
Sql functions
Sql functionsSql functions
Sql functions
 

More from Balqees Al.Mubarak (9)

Using scripts
Using scriptsUsing scripts
Using scripts
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Oracle views
Oracle viewsOracle views
Oracle views
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Using social network sites
Using social network sites Using social network sites
Using social network sites
 

Recently uploaded

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 

Recently uploaded (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 

Single row functions