SlideShare a Scribd company logo
SQL FUNCTIONS
What Is a Function?
A function is a programming unit returning a single value, allowing values to
be passed in as parameters. The parameters can change the outcome or return
the result of a function. The beauty of a function is that it is self-contained
and can thus be embedded in an expression.
By definition, in Oracle SQL an expression is a SQL code command or even
another function.
SQL Functions
Functions are very powerful feature of SQL and can be used to do the
following:
• Perform calculations on dala
• Modify individual data items
• Manipulate output for groups of rows
• Format dates and numbers for display
• Convert column datatypes
SQL functions may accept arguments and always retum a value.
Note: Most of the functions described in this lesson are specific to Oracle's
versions.
1
Types of Functions
In general, functions in Oracle SQL are divided into five groups, the first
group being the topic of this chapter, namely, single row functions. Other
function types are aggregate functions, which create groups of rows; analytic
functions, which also group but allow in-depth data analysis; object reference
functions, allowing access to special object pointers; and finally user-defined
functions, such as those you create using a programming language such as
PL/SQL.
• Single Row Functions— Single row functions can be used to execute
an operation on each row of a query. In other words, a single row
function can be used to execute the same operation for every row a
query retrieves.
• Aggregate Functions— These functions summarize repeating groups
in a row set into distinct groups, aggregating repetitive values into
items such as sums or averages.
• Analytic Functions— Unlike aggregates, which summarize repetitions
into unique items, analytics create subset summaries within aggregates.
• Object Reference Functions— These functions use pointers to
reference values. Commonly, object reference functions either
reference objects or dereference values from objects.
• User-Defined Functions— Custom functions can be built using
PL/SQL, allowing extension of the large library of Oracle SQL built-in
functionality. PL/SQL is beyond the scope of this book..
2
Single Row Functions
Now let's focus on the subject matter of this chapter. Recall that a single row
function was defined as a function that can be used to execute an operation on
each row of a query.
Let's start this journey by discovering the different classifications for single
row functions:
String Functions These functions take a string as a parameter
and return a number or a string.
Number Functions A number is passed in, usually returning a
number.
Datetime Functions These functions accept date value parameters.
Conversion Functions These functions convert between different
datatypes.
Miscellaneous Functions Odd functions fall into this category.
User-defined Functions Functions created by a user via PL/SQL
3
User-Defined Functions
A user-defined function is a function created by a user in addition to the
Oracle built-in functions. Believe it or not, sometimes the plethora of
available Oracle built-in functions will not suffice. Creating your own
functions can be done using PL/SQL.
4
CHARACTER FUNCTIONS
Character functions operate on values of character class datatype, i.e., Char,
Varchar2, Varchar etc. These functions can return either character class
datatype or number classs datatype based on the operation performed on the
input data. Length of a value returned by these functions is limited upto 4000
bytes for varchar2 datatype and 2000 bytes for char datatype. If a function
returns a value that exceedes the length limit, Oracle automatically truncate
the value before returning the result. Some of the SQL built-in character
functions are given in the following table.
Single-row Character Functions
CHR CONCAT INITCAP SUBSTR
RTRIM LTRIM TRIM REPLACE
LPAD RPAD UPPER LOWER
TRANSLATE ASCII INSTR LENGTH
5
Examples
SELECT ASCII('Ç')
FROM DUAL;
ASCII ( 'Ç' )
199
SELECT CHR(76) "KARAKTER"
FROM DUAL ;
KAR
L
SELECT ENAME, CONCAT(ENAME, JOB), LENGTH(ename),
INSTR(ename, 'A' )
FROM EMP
WHERE SUBSTR(job ,1 , 5) = UPPER('saLEs');
ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A')
ALLEN ALLENSALESMAN 5 1
WARD WARDSALESMAN 4 2
MARTIN MARTINSALESMAN 6 2
TURNER TURNERSALESMAN 6 0
SELECT ENAME, CONCAT(ENAME, JOB), LENGTH(ename),
INSTR(ename, 'A' )
FROM EMP
WHERE SUBSTR(ename , -1 , 1) = 'N' ;
ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A')
ALLEN ALLENSALESMAN 5 1
MARTIN MARTINSALESMAN 6 2
6
SELECT INSTR('MISSISSIPPI' , 'S' ,5 , 2)
FROM DUAL;
INSTR('MISSISSIPPI','S',5,2)
7
SELECT LPAD( ENAME , 20 , '*' )
FROM emp;
LPAD(ENAME,20,'*')
***************SMITH
***************ALLEN
14 rows selected.
SELECT RPAD( ENAME , 20 , '*' )
FROM EMP;
RPAD(ENAME,20,'*')
SMITH***************
ALLEN***************
14 rows selected.
SELECT LTRIM('aaaaabbccxXXyyzzaaabbbccc' , 'a') Ltrim
FROM DUAL;
LTRIM
bbccxXXyyzzaaabbbccc
SELECT RTRIM('aaaaabbccxXXyyzzaaabbbcccaaaaaaaa' , 'a') Rtrim
FROM DUAL;
RTRIM
aaaaabbccxXXyyzzaaabbbccc
SELECT ENAME
7
FROM EMP
WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE');
ENAME
SMITH
SELECT SUBSTR('ABCDEFGHIJK' ,4,3) "Alt Metin"
FROM DUAL;
Alt Metin
DEF
Character functions:
A. Case Conversion Functions
• LOWER
• UPPER
• INITCAP
B. Character Manipulation Functions
• CONCAT
• SUBSTR
• LENGTH
• INSTR
• LPAD
• RPAD
• TRIM
• LTRIM
• RTRIM
• ASCII
8
• CHR
• REPLACE
• TRANSLATE
9
Case Conversion Functions
Convert case for character strings
Function Result
LOWER('SQL Course')
UPPER('SQLCourse')
INITCAP('SQLCourse'}
sql course
SQL COURSE
Sql Course
Case Conversion Functions
LOWER, UPPER, and INITCAP are the three case conversion functions.
• LOWER Converts mıxed case ör uppercass character string lo
louercase
• UPPER Converts mısed case ör louercasc clıaracter string to
uppcrcase
• INITCAP Converts first letter of each word to uppercase and
rcmainig Icttcrs to lowercase.
10
Using Case Conversion Functions
Display the employee number, name, and department number for
employee Blake.
SELECT empno, ename, deptno
FROM emp
WHERE ename = 'blake';
no rows selected
The WHERE clause of the first SQL statement specifies the employee name
as ' blake.' Since all the data in the EMP tabls is stored in uppercase. the
name ' blake' does not ffind a match in the EMP table and as a result no rows
are selected.
..........................................................
SELECT empno, ename, deptno
FROM emp
WHERE LOWER( ename) = 'blake’
;
EMPNO ENAME DEPTNO
7698 BLAKE 30
The WHERE clause of the second SQL statement specifies that the employee
name in the EMP table be converted to lowercase and then be compared to
'blake ' . Since both the names are in lowercase now, a match is found and
one row is selected. The WHERE clause can be rewritten in the following
manner to produce the same result:
11
Using Character Manipulation Functions
Manipulate character strings
Function Result
CONCAT( ‘Good' , 'String' )
SUBSTR( 'String' , 1,3 )
LENGTH( 'String' )
INSTR(' String’, ‘r’)
LPAD(sal,10 , '*' )
GoodString
Str
6
3
******5000
Character Manipulation Functions
CONCAT. SUBSTR, LENGTH, INSTR. and LPAD are îhe five character
manipulation functions covered in this lesson.
CONCAT Joins values together (Yon are limited to using two parameters
with CONCAT.)
SUBSTR Extracts a string of determined length
LENGTH Shows the length of a string as a numeric value
INSTR Finds numerîc position of a named character
LPAD Pads the character value right-justified
Note: RPAD character manipulation function pads the character value lelt-
justified.
Using the Character Manipulation
Functions (continued)
Example:
The slide example displays ernployee name and job joined together,
length of the employee name, and the numeric position of the letter A in
the employee name, forall employees who are in sales.
SELECT ename, CONCAT( ename, job),
LENGTH(ename),
INSTR(ename, 'A')
FROM emp
WHERE SUBSTR(job, 1, 5) = 'SALES' ;
ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A')
ALLEN ALLENSALESMAN 5 1
WARD WARDSALESMAN 4 2
MARTIN MARTINSALESMAN 6 2
TURNER TURNERSALESMAN 6 0
Using the Character Manipulation
Functions (continued)
Example
Write a SQL statement to display the data for those employees whose
names end with an N .
SELECT ename, CONCAT(ename, job),
LENGTH(ename), INSTR(ename, 'A')
FROM emp
WHERE SUBSTR(ename, -1, 1) = 'N';
ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A')
ALLEN ALLENSALESMAN 5 1
MARTIN MARTINSALESMAN 6 2

More Related Content

What's hot

Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
Shahadat153031
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
Dhananjay Goel
 
Sql commands
Sql commandsSql commands
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
Satya P. Joshi
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
sunanditaAnand
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
PadamNepal1
 
DBMS: Types of keys
DBMS:  Types of keysDBMS:  Types of keys
DBMS: Types of keys
Bharati Ugale
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
Anusha sivakumar
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
Swapnali Pawar
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
Amrit Kaur
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
Salman Memon
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
DataminingTools Inc
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
Smriti Jain
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR
 

What's hot (20)

Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Sql commands
Sql commandsSql commands
Sql commands
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 
DBMS: Types of keys
DBMS:  Types of keysDBMS:  Types of keys
DBMS: Types of keys
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 

Similar to Sql functions

C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
SQL Function.ppt
SQL Function.pptSQL Function.ppt
SQL Function.ppt
HappyFace22
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
RickyLidar
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
Mehwish Mehmood
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
JAX London
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
Brunel University
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Meghaj Mallick
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
MalligaarjunanN
 
Function
FunctionFunction
Function
Saniati
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
CindyCuesta
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sql
pgolhar
 
Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
Eberhard Wolff
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
dipumaliy
 
Function
FunctionFunction
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
Abhiram Vijay
 

Similar to Sql functions (20)

C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
SQL Function.ppt
SQL Function.pptSQL Function.ppt
SQL Function.ppt
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
Function
FunctionFunction
Function
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sql
 
Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
 
Function
FunctionFunction
Function
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
 

More from Ankit Dubey

Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}
Ankit Dubey
 
Scheduling
Scheduling Scheduling
Scheduling
Ankit Dubey
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
Ankit Dubey
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
Ankit Dubey
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
Ankit Dubey
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
Ankit Dubey
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
Ankit Dubey
 
Ch5 cpu-scheduling
Ch5 cpu-schedulingCh5 cpu-scheduling
Ch5 cpu-scheduling
Ankit Dubey
 
Ch4 threads
Ch4 threadsCh4 threads
Ch4 threads
Ankit Dubey
 
Ch3 processes
Ch3 processesCh3 processes
Ch3 processes
Ankit Dubey
 
Ch2 system structure
Ch2 system structureCh2 system structure
Ch2 system structure
Ankit Dubey
 
Ch1 introduction-to-os
Ch1 introduction-to-osCh1 introduction-to-os
Ch1 introduction-to-os
Ankit Dubey
 
Android i
Android iAndroid i
Android i
Ankit Dubey
 
Mongodb mock test_ii
Mongodb mock test_iiMongodb mock test_ii
Mongodb mock test_ii
Ankit Dubey
 
Android mock test_iii
Android mock test_iiiAndroid mock test_iii
Android mock test_iii
Ankit Dubey
 
Android mock test_ii
Android mock test_iiAndroid mock test_ii
Android mock test_ii
Ankit Dubey
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
Ankit Dubey
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
Ankit Dubey
 
Ajp notes-chapter-04
Ajp notes-chapter-04Ajp notes-chapter-04
Ajp notes-chapter-04
Ankit Dubey
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
Ankit Dubey
 

More from Ankit Dubey (20)

Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}
 
Scheduling
Scheduling Scheduling
Scheduling
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Ch5 cpu-scheduling
Ch5 cpu-schedulingCh5 cpu-scheduling
Ch5 cpu-scheduling
 
Ch4 threads
Ch4 threadsCh4 threads
Ch4 threads
 
Ch3 processes
Ch3 processesCh3 processes
Ch3 processes
 
Ch2 system structure
Ch2 system structureCh2 system structure
Ch2 system structure
 
Ch1 introduction-to-os
Ch1 introduction-to-osCh1 introduction-to-os
Ch1 introduction-to-os
 
Android i
Android iAndroid i
Android i
 
Mongodb mock test_ii
Mongodb mock test_iiMongodb mock test_ii
Mongodb mock test_ii
 
Android mock test_iii
Android mock test_iiiAndroid mock test_iii
Android mock test_iii
 
Android mock test_ii
Android mock test_iiAndroid mock test_ii
Android mock test_ii
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
 
Ajp notes-chapter-04
Ajp notes-chapter-04Ajp notes-chapter-04
Ajp notes-chapter-04
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 

Recently uploaded

Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 

Recently uploaded (20)

Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 

Sql functions

  • 1. SQL FUNCTIONS What Is a Function? A function is a programming unit returning a single value, allowing values to be passed in as parameters. The parameters can change the outcome or return the result of a function. The beauty of a function is that it is self-contained and can thus be embedded in an expression. By definition, in Oracle SQL an expression is a SQL code command or even another function. SQL Functions Functions are very powerful feature of SQL and can be used to do the following: • Perform calculations on dala • Modify individual data items • Manipulate output for groups of rows • Format dates and numbers for display • Convert column datatypes SQL functions may accept arguments and always retum a value. Note: Most of the functions described in this lesson are specific to Oracle's versions. 1
  • 2. Types of Functions In general, functions in Oracle SQL are divided into five groups, the first group being the topic of this chapter, namely, single row functions. Other function types are aggregate functions, which create groups of rows; analytic functions, which also group but allow in-depth data analysis; object reference functions, allowing access to special object pointers; and finally user-defined functions, such as those you create using a programming language such as PL/SQL. • Single Row Functions— Single row functions can be used to execute an operation on each row of a query. In other words, a single row function can be used to execute the same operation for every row a query retrieves. • Aggregate Functions— These functions summarize repeating groups in a row set into distinct groups, aggregating repetitive values into items such as sums or averages. • Analytic Functions— Unlike aggregates, which summarize repetitions into unique items, analytics create subset summaries within aggregates. • Object Reference Functions— These functions use pointers to reference values. Commonly, object reference functions either reference objects or dereference values from objects. • User-Defined Functions— Custom functions can be built using PL/SQL, allowing extension of the large library of Oracle SQL built-in functionality. PL/SQL is beyond the scope of this book.. 2
  • 3. Single Row Functions Now let's focus on the subject matter of this chapter. Recall that a single row function was defined as a function that can be used to execute an operation on each row of a query. Let's start this journey by discovering the different classifications for single row functions: String Functions These functions take a string as a parameter and return a number or a string. Number Functions A number is passed in, usually returning a number. Datetime Functions These functions accept date value parameters. Conversion Functions These functions convert between different datatypes. Miscellaneous Functions Odd functions fall into this category. User-defined Functions Functions created by a user via PL/SQL 3
  • 4. User-Defined Functions A user-defined function is a function created by a user in addition to the Oracle built-in functions. Believe it or not, sometimes the plethora of available Oracle built-in functions will not suffice. Creating your own functions can be done using PL/SQL. 4
  • 5. CHARACTER FUNCTIONS Character functions operate on values of character class datatype, i.e., Char, Varchar2, Varchar etc. These functions can return either character class datatype or number classs datatype based on the operation performed on the input data. Length of a value returned by these functions is limited upto 4000 bytes for varchar2 datatype and 2000 bytes for char datatype. If a function returns a value that exceedes the length limit, Oracle automatically truncate the value before returning the result. Some of the SQL built-in character functions are given in the following table. Single-row Character Functions CHR CONCAT INITCAP SUBSTR RTRIM LTRIM TRIM REPLACE LPAD RPAD UPPER LOWER TRANSLATE ASCII INSTR LENGTH 5
  • 6. Examples SELECT ASCII('Ç') FROM DUAL; ASCII ( 'Ç' ) 199 SELECT CHR(76) "KARAKTER" FROM DUAL ; KAR L SELECT ENAME, CONCAT(ENAME, JOB), LENGTH(ename), INSTR(ename, 'A' ) FROM EMP WHERE SUBSTR(job ,1 , 5) = UPPER('saLEs'); ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A') ALLEN ALLENSALESMAN 5 1 WARD WARDSALESMAN 4 2 MARTIN MARTINSALESMAN 6 2 TURNER TURNERSALESMAN 6 0 SELECT ENAME, CONCAT(ENAME, JOB), LENGTH(ename), INSTR(ename, 'A' ) FROM EMP WHERE SUBSTR(ename , -1 , 1) = 'N' ; ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A') ALLEN ALLENSALESMAN 5 1 MARTIN MARTINSALESMAN 6 2 6
  • 7. SELECT INSTR('MISSISSIPPI' , 'S' ,5 , 2) FROM DUAL; INSTR('MISSISSIPPI','S',5,2) 7 SELECT LPAD( ENAME , 20 , '*' ) FROM emp; LPAD(ENAME,20,'*') ***************SMITH ***************ALLEN 14 rows selected. SELECT RPAD( ENAME , 20 , '*' ) FROM EMP; RPAD(ENAME,20,'*') SMITH*************** ALLEN*************** 14 rows selected. SELECT LTRIM('aaaaabbccxXXyyzzaaabbbccc' , 'a') Ltrim FROM DUAL; LTRIM bbccxXXyyzzaaabbbccc SELECT RTRIM('aaaaabbccxXXyyzzaaabbbcccaaaaaaaa' , 'a') Rtrim FROM DUAL; RTRIM aaaaabbccxXXyyzzaaabbbccc SELECT ENAME 7
  • 8. FROM EMP WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE'); ENAME SMITH SELECT SUBSTR('ABCDEFGHIJK' ,4,3) "Alt Metin" FROM DUAL; Alt Metin DEF Character functions: A. Case Conversion Functions • LOWER • UPPER • INITCAP B. Character Manipulation Functions • CONCAT • SUBSTR • LENGTH • INSTR • LPAD • RPAD • TRIM • LTRIM • RTRIM • ASCII 8
  • 10. Case Conversion Functions Convert case for character strings Function Result LOWER('SQL Course') UPPER('SQLCourse') INITCAP('SQLCourse'} sql course SQL COURSE Sql Course Case Conversion Functions LOWER, UPPER, and INITCAP are the three case conversion functions. • LOWER Converts mıxed case ör uppercass character string lo louercase • UPPER Converts mısed case ör louercasc clıaracter string to uppcrcase • INITCAP Converts first letter of each word to uppercase and rcmainig Icttcrs to lowercase. 10
  • 11. Using Case Conversion Functions Display the employee number, name, and department number for employee Blake. SELECT empno, ename, deptno FROM emp WHERE ename = 'blake'; no rows selected The WHERE clause of the first SQL statement specifies the employee name as ' blake.' Since all the data in the EMP tabls is stored in uppercase. the name ' blake' does not ffind a match in the EMP table and as a result no rows are selected. .......................................................... SELECT empno, ename, deptno FROM emp WHERE LOWER( ename) = 'blake’ ; EMPNO ENAME DEPTNO 7698 BLAKE 30 The WHERE clause of the second SQL statement specifies that the employee name in the EMP table be converted to lowercase and then be compared to 'blake ' . Since both the names are in lowercase now, a match is found and one row is selected. The WHERE clause can be rewritten in the following manner to produce the same result: 11
  • 12. Using Character Manipulation Functions Manipulate character strings Function Result CONCAT( ‘Good' , 'String' ) SUBSTR( 'String' , 1,3 ) LENGTH( 'String' ) INSTR(' String’, ‘r’) LPAD(sal,10 , '*' ) GoodString Str 6 3 ******5000 Character Manipulation Functions CONCAT. SUBSTR, LENGTH, INSTR. and LPAD are îhe five character manipulation functions covered in this lesson. CONCAT Joins values together (Yon are limited to using two parameters with CONCAT.) SUBSTR Extracts a string of determined length LENGTH Shows the length of a string as a numeric value INSTR Finds numerîc position of a named character LPAD Pads the character value right-justified Note: RPAD character manipulation function pads the character value lelt- justified.
  • 13. Using the Character Manipulation Functions (continued) Example: The slide example displays ernployee name and job joined together, length of the employee name, and the numeric position of the letter A in the employee name, forall employees who are in sales. SELECT ename, CONCAT( ename, job), LENGTH(ename), INSTR(ename, 'A') FROM emp WHERE SUBSTR(job, 1, 5) = 'SALES' ; ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A') ALLEN ALLENSALESMAN 5 1 WARD WARDSALESMAN 4 2 MARTIN MARTINSALESMAN 6 2 TURNER TURNERSALESMAN 6 0
  • 14. Using the Character Manipulation Functions (continued) Example Write a SQL statement to display the data for those employees whose names end with an N . SELECT ename, CONCAT(ename, job), LENGTH(ename), INSTR(ename, 'A') FROM emp WHERE SUBSTR(ename, -1, 1) = 'N'; ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A') ALLEN ALLENSALESMAN 5 1 MARTIN MARTINSALESMAN 6 2