SlideShare a Scribd company logo
1 of 5
Download to read offline
8/10/13 Oracle SQL Functions
beginner-sql-tutorial.com/oracle-functions.htm 1/5
Oracle Built in Functions
There are two types of functions in Oracle.
1)Single Row Functions: Single row or Scalar functions return a value for every row that is processed in a
query.
2)Group Functions: These functions group the rows of data based on the values returned by the query. This is
discussed in SQL GROUP Functions. The group functions are used to calculate aggregate values like total or
average, which return just one total or one average value after processing a group of rows.
There are four types of single row functions. They are:
1)Numeric Functions: These are functions that accept numeric input and return numeric values.
2)Character or Text Functions: These are functions that accept character input and can return both
character and number values.
3)Date Functions: These are functions that take values that are of datatype DATE as input and return values
of datatype DATE, except for the MONTHS_BETWEEN function, which returns a number.
4)Conversion Functions: These are functions that help us to convert a value in one form to another form. For
Example: a null value into an actual value, or a value from one datatype to another datatype like NVL,
TO_CHAR, TO_NUMBER, TO_DATE etc.
You can combine more than one function together in an expression. This is known as nesting of functions.
What is a DUAL Table in Oracle?
This is a single row and single column dummy table provided by oracle. This is used to perform mathematical
calculations without using a table.
Select * from DUAL
Output:
DUMMY
-------
X
8/10/13 Oracle SQL Functions
beginner-sql-tutorial.com/oracle-functions.htm 2/5
Select 777 * 888 from Dual
Output:
777 * 888
---------
689976
1) Numeric Functions:
Numeric functions are used to perform operations on numbers. They accept numeric values as input and return
numeric values as output. Few of the Numeric functions are:
Function
Name
Return Value
ABS (x) Absolute value of the number 'x'
CEIL (x) Integer value that is Greater than or equal to the number 'x'
FLOOR (x) Integer value that is Less than or equal to the number 'x'
TRUNC (x,
y)
Truncates value of number 'x' up to 'y' decimal places
ROUND (x,
y)
Rounded off value of the number 'x' up to the number 'y' decimal
places
The following examples explains the usage of the above numeric functions
Function
Name
Examples
Return
Value
ABS (x)
ABS (1)
ABS (-1)
1
-1
CEIL (x)
CEIL (2.83)
CEIL (2.49)
CEIL (-1.6)
3
3
-1
FLOOR (x)
FLOOR (2.83)
FLOOR (2.49)
FLOOR (-1.6)
2
2
-2
TRUNC (x, y)
ROUND (125.456, 1)
ROUND (125.456, 0)
ROUND (124.456, -1)
125.4
125
120
ROUND (x, y)
TRUNC (140.234, 2)
TRUNC (-54, 1)
TRUNC (5.7)
TRUNC (142, -1)
140.23
54
5
140
These functions can be used on database columns.
For Example: Let's consider the product table used in sql joins. We can use ROUND to round off the unit_price to
the nearest integer, if any product has prices in fraction.
SELECT ROUND (unit_price) FROM product;
8/10/13 Oracle SQL Functions
beginner-sql-tutorial.com/oracle-functions.htm 3/5
2) Character or Text Functions:
Character or text functions are used to manipulate text strings. They accept strings or characters as input and
can return both character and number values as output.
Few of the character or text functions are as given below:
Function Name Return Value
LOWER
(string_value)
All the letters in 'string_value' is converted to lowercase.
UPPER
(string_value)
All the letters in 'string_value' is converted to uppercase.
INITCAP
(string_value)
All the letters in 'string_value' is converted to mixed case.
LTRIM
(string_value,
trim_text)
All occurrences of 'trim_text' is removed from the left
of 'string_value'.
RTRIM
(string_value,
trim_text)
All occurrences of 'trim_text' is removed from the right
of'string_value' .
TRIM (trim_text
FROM
string_value)
All occurrences of 'trim_text' from the left and right
of 'string_value' ,'trim_text' can also be only one character
long .
SUBSTR
(string_value, m,
n)
Returns 'n' number of characters from'string_value' starting
from the 'm'position.
LENGTH
(string_value)
Number of characters in 'string_value'in returned.
LPAD
(string_value, n,
pad_value)
Returns 'string_value' left-padded with'pad_value' . The
length of the whole string will be of 'n' characters.
RPAD
(string_value, n,
pad_value)
Returns 'string_value' right-padded with 'pad_value' . The
length of the whole string will be of 'n' characters.
For Example, we can use the above UPPER() text function with the column value as follows.
SELECT UPPER (product_name) FROM product;
The following examples explains the usage of the above character or text functions
Function Name Examples Return Value
LOWER(string_value) LOWER('Good Morning') good morning
UPPER(string_value) UPPER('Good Morning')
GOOD
MORNING
INITCAP(string_value) INITCAP('GOOD MORNING') Good Morning
LTRIM(string_value, trim_text) LTRIM ('Good Morning', 'Good) Morning
RTRIM (string_value, trim_text)
RTRIM ('Good Morning', '
Morning')
Good
8/10/13 Oracle SQL Functions
beginner-sql-tutorial.com/oracle-functions.htm 4/5
TRIM (trim_text FROM
string_value)
TRIM ('o' FROM 'Good
Morning')
Gd Mrning
SUBSTR (string_value, m, n) SUBSTR ('Good Morning', 6, 7) Morning
LENGTH (string_value) LENGTH ('Good Morning') 12
LPAD (string_value, n,
pad_value)
LPAD ('Good', 6, '*') **Good
RPAD (string_value, n,
pad_value)
RPAD ('Good', 6, '*') Good**
3) Date Functions:
These are functions that take values that are of datatype DATE as input and return values of datatypes DATE,
except for the MONTHS_BETWEEN function, which returns a number as output.
Few date functions are as given below.
Function Name Return Value
ADD_MONTHS
(date, n)
Returns a date value after adding 'n'months to the
date 'x'.
MONTHS_BETWEEN
(x1, x2)
Returns the number of months between dates x1 and x2.
ROUND (x,
date_format)
Returns the date 'x' rounded off to the nearest century,
year, month, date, hour, minute, or second as specified
by the 'date_format'.
TRUNC (x,
date_format)
Returns the date 'x' lesser than or equal to the nearest
century, year, month, date, hour, minute, or second as
specified by the 'date_format'.
NEXT_DAY (x,
week_day)
Returns the next date of the 'week_day'on or after the
date 'x' occurs.
LAST_DAY (x)
It is used to determine the number of days remaining in a
month from the date 'x' specified.
SYSDATE Returns the systems current date and time.
NEW_TIME (x,
zone1, zone2)
Returns the date and time in zone2 if date 'x' represents
the time in zone1.
The below table provides the examples for the above functions
Function Name Examples
Return
Value
ADD_MONTHS ( ) ADD_MONTHS ('16-Sep-81', 3) 16-Dec-81
MONTHS_BETWEEN(
)
MONTHS_BETWEEN ('16-Sep-81', '16-Dec-
81')
3
NEXT_DAY( ) NEXT_DAY ('01-Jun-08', 'Wednesday') 04-JUN-08
LAST_DAY( ) LAST_DAY ('01-Jun-08') 30-Jun-08
NEW_TIME( ) NEW_TIME ('01-Jun-08', 'IST', 'EST') 31-May-08
4) Conversion Functions:
These are functions that help us to convert a value in one form to another form. For Ex: a null value into an
actual value, or a value from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER, TO_DATE.
8/10/13 Oracle SQL Functions
beginner-sql-tutorial.com/oracle-functions.htm 5/5
Few of the conversion functions available in oracle are:
Function Name Return Value
TO_CHAR (x [,y])
Converts Numeric and Date values to a character string
value. It cannot be used for calculations since it is a string
value.
TO_DATE (x [,
date_format])
Converts a valid Numeric and Character values to a Date
value. Date is formatted to the format specified
by 'date_format'.
NVL (x, y)
If 'x' is NULL, replace it with 'y'. 'x' and 'y'must be of the
same datatype.
DECODE (a, b, c,
d, e,
default_value)
Checks the value of 'a', if a = b, then returns'c'. If a = d,
then returns 'e'. Else, returnsdefault_value.
The below table provides the examples for the above functions
Function
Name
Examples Return Value
TO_CHAR ()
TO_CHAR (3000, '$9999')
TO_CHAR (SYSDATE, 'Day, Month YYYY')
$3000
Monday, June 2008
TO_DATE () TO_DATE ('01-Jun-08') 01-Jun-08
NVL () NVL (null, 1) 1

More Related Content

What's hot (20)

Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
Arrays
ArraysArrays
Arrays
 
Matlab quickref
Matlab quickrefMatlab quickref
Matlab quickref
 
Arrays
ArraysArrays
Arrays
 
Recursion Lecture in C++
Recursion Lecture in C++Recursion Lecture in C++
Recursion Lecture in C++
 
Arrays
ArraysArrays
Arrays
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Sql functions
Sql functionsSql functions
Sql functions
 
Lec3
Lec3Lec3
Lec3
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Recursion Lecture in Java
Recursion Lecture in JavaRecursion Lecture in Java
Recursion Lecture in Java
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
PCA and LDA in machine learning
PCA and LDA in machine learningPCA and LDA in machine learning
PCA and LDA in machine learning
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 

Viewers also liked

Viewers also liked (13)

Analytica acta - 2012
Analytica acta - 2012Analytica acta - 2012
Analytica acta - 2012
 
Terrorism main book chapter by ogbaji
Terrorism main book chapter by ogbajiTerrorism main book chapter by ogbaji
Terrorism main book chapter by ogbaji
 
Conference on Diabetes and Metabolism
Conference on Diabetes and MetabolismConference on Diabetes and Metabolism
Conference on Diabetes and Metabolism
 
Conference on Addiction Research & Therapy
Conference on Addiction Research & TherapyConference on Addiction Research & Therapy
Conference on Addiction Research & Therapy
 
Education is the key to success
Education is the key to successEducation is the key to success
Education is the key to success
 
Conferences
ConferencesConferences
Conferences
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Tyra Banks nose job
Tyra Banks nose jobTyra Banks nose job
Tyra Banks nose job
 
Virology - 2011
Virology - 2011Virology - 2011
Virology - 2011
 
Cosmetology - 2012
Cosmetology - 2012Cosmetology - 2012
Cosmetology - 2012
 
Android and Android Phones
Android and Android PhonesAndroid and Android Phones
Android and Android Phones
 
Liver Injury linked to Flavocoxid
Liver Injury linked to FlavocoxidLiver Injury linked to Flavocoxid
Liver Injury linked to Flavocoxid
 
Nanotek - 2012
Nanotek - 2012Nanotek - 2012
Nanotek - 2012
 

Similar to Oracle sql functions

Similar to Oracle sql functions (20)

Oracle sql ppt2
Oracle sql ppt2Oracle sql ppt2
Oracle sql ppt2
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
Mysql1
Mysql1Mysql1
Mysql1
 
SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sql
 
sql functions3 (1).pdf
sql functions3 (1).pdfsql functions3 (1).pdf
sql functions3 (1).pdf
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Functions
FunctionsFunctions
Functions
 
Tableau functions
Tableau   functionsTableau   functions
Tableau functions
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 
Sql functions
Sql functionsSql functions
Sql functions
 
Foxpro (1)
Foxpro (1)Foxpro (1)
Foxpro (1)
 
Hive function-cheat-sheet
Hive function-cheat-sheetHive function-cheat-sheet
Hive function-cheat-sheet
 

More from Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 

More from Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Oracle sql functions

  • 1. 8/10/13 Oracle SQL Functions beginner-sql-tutorial.com/oracle-functions.htm 1/5 Oracle Built in Functions There are two types of functions in Oracle. 1)Single Row Functions: Single row or Scalar functions return a value for every row that is processed in a query. 2)Group Functions: These functions group the rows of data based on the values returned by the query. This is discussed in SQL GROUP Functions. The group functions are used to calculate aggregate values like total or average, which return just one total or one average value after processing a group of rows. There are four types of single row functions. They are: 1)Numeric Functions: These are functions that accept numeric input and return numeric values. 2)Character or Text Functions: These are functions that accept character input and can return both character and number values. 3)Date Functions: These are functions that take values that are of datatype DATE as input and return values of datatype DATE, except for the MONTHS_BETWEEN function, which returns a number. 4)Conversion Functions: These are functions that help us to convert a value in one form to another form. For Example: a null value into an actual value, or a value from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER, TO_DATE etc. You can combine more than one function together in an expression. This is known as nesting of functions. What is a DUAL Table in Oracle? This is a single row and single column dummy table provided by oracle. This is used to perform mathematical calculations without using a table. Select * from DUAL Output: DUMMY ------- X
  • 2. 8/10/13 Oracle SQL Functions beginner-sql-tutorial.com/oracle-functions.htm 2/5 Select 777 * 888 from Dual Output: 777 * 888 --------- 689976 1) Numeric Functions: Numeric functions are used to perform operations on numbers. They accept numeric values as input and return numeric values as output. Few of the Numeric functions are: Function Name Return Value ABS (x) Absolute value of the number 'x' CEIL (x) Integer value that is Greater than or equal to the number 'x' FLOOR (x) Integer value that is Less than or equal to the number 'x' TRUNC (x, y) Truncates value of number 'x' up to 'y' decimal places ROUND (x, y) Rounded off value of the number 'x' up to the number 'y' decimal places The following examples explains the usage of the above numeric functions Function Name Examples Return Value ABS (x) ABS (1) ABS (-1) 1 -1 CEIL (x) CEIL (2.83) CEIL (2.49) CEIL (-1.6) 3 3 -1 FLOOR (x) FLOOR (2.83) FLOOR (2.49) FLOOR (-1.6) 2 2 -2 TRUNC (x, y) ROUND (125.456, 1) ROUND (125.456, 0) ROUND (124.456, -1) 125.4 125 120 ROUND (x, y) TRUNC (140.234, 2) TRUNC (-54, 1) TRUNC (5.7) TRUNC (142, -1) 140.23 54 5 140 These functions can be used on database columns. For Example: Let's consider the product table used in sql joins. We can use ROUND to round off the unit_price to the nearest integer, if any product has prices in fraction. SELECT ROUND (unit_price) FROM product;
  • 3. 8/10/13 Oracle SQL Functions beginner-sql-tutorial.com/oracle-functions.htm 3/5 2) Character or Text Functions: Character or text functions are used to manipulate text strings. They accept strings or characters as input and can return both character and number values as output. Few of the character or text functions are as given below: Function Name Return Value LOWER (string_value) All the letters in 'string_value' is converted to lowercase. UPPER (string_value) All the letters in 'string_value' is converted to uppercase. INITCAP (string_value) All the letters in 'string_value' is converted to mixed case. LTRIM (string_value, trim_text) All occurrences of 'trim_text' is removed from the left of 'string_value'. RTRIM (string_value, trim_text) All occurrences of 'trim_text' is removed from the right of'string_value' . TRIM (trim_text FROM string_value) All occurrences of 'trim_text' from the left and right of 'string_value' ,'trim_text' can also be only one character long . SUBSTR (string_value, m, n) Returns 'n' number of characters from'string_value' starting from the 'm'position. LENGTH (string_value) Number of characters in 'string_value'in returned. LPAD (string_value, n, pad_value) Returns 'string_value' left-padded with'pad_value' . The length of the whole string will be of 'n' characters. RPAD (string_value, n, pad_value) Returns 'string_value' right-padded with 'pad_value' . The length of the whole string will be of 'n' characters. For Example, we can use the above UPPER() text function with the column value as follows. SELECT UPPER (product_name) FROM product; The following examples explains the usage of the above character or text functions Function Name Examples Return Value LOWER(string_value) LOWER('Good Morning') good morning UPPER(string_value) UPPER('Good Morning') GOOD MORNING INITCAP(string_value) INITCAP('GOOD MORNING') Good Morning LTRIM(string_value, trim_text) LTRIM ('Good Morning', 'Good) Morning RTRIM (string_value, trim_text) RTRIM ('Good Morning', ' Morning') Good
  • 4. 8/10/13 Oracle SQL Functions beginner-sql-tutorial.com/oracle-functions.htm 4/5 TRIM (trim_text FROM string_value) TRIM ('o' FROM 'Good Morning') Gd Mrning SUBSTR (string_value, m, n) SUBSTR ('Good Morning', 6, 7) Morning LENGTH (string_value) LENGTH ('Good Morning') 12 LPAD (string_value, n, pad_value) LPAD ('Good', 6, '*') **Good RPAD (string_value, n, pad_value) RPAD ('Good', 6, '*') Good** 3) Date Functions: These are functions that take values that are of datatype DATE as input and return values of datatypes DATE, except for the MONTHS_BETWEEN function, which returns a number as output. Few date functions are as given below. Function Name Return Value ADD_MONTHS (date, n) Returns a date value after adding 'n'months to the date 'x'. MONTHS_BETWEEN (x1, x2) Returns the number of months between dates x1 and x2. ROUND (x, date_format) Returns the date 'x' rounded off to the nearest century, year, month, date, hour, minute, or second as specified by the 'date_format'. TRUNC (x, date_format) Returns the date 'x' lesser than or equal to the nearest century, year, month, date, hour, minute, or second as specified by the 'date_format'. NEXT_DAY (x, week_day) Returns the next date of the 'week_day'on or after the date 'x' occurs. LAST_DAY (x) It is used to determine the number of days remaining in a month from the date 'x' specified. SYSDATE Returns the systems current date and time. NEW_TIME (x, zone1, zone2) Returns the date and time in zone2 if date 'x' represents the time in zone1. The below table provides the examples for the above functions Function Name Examples Return Value ADD_MONTHS ( ) ADD_MONTHS ('16-Sep-81', 3) 16-Dec-81 MONTHS_BETWEEN( ) MONTHS_BETWEEN ('16-Sep-81', '16-Dec- 81') 3 NEXT_DAY( ) NEXT_DAY ('01-Jun-08', 'Wednesday') 04-JUN-08 LAST_DAY( ) LAST_DAY ('01-Jun-08') 30-Jun-08 NEW_TIME( ) NEW_TIME ('01-Jun-08', 'IST', 'EST') 31-May-08 4) Conversion Functions: These are functions that help us to convert a value in one form to another form. For Ex: a null value into an actual value, or a value from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER, TO_DATE.
  • 5. 8/10/13 Oracle SQL Functions beginner-sql-tutorial.com/oracle-functions.htm 5/5 Few of the conversion functions available in oracle are: Function Name Return Value TO_CHAR (x [,y]) Converts Numeric and Date values to a character string value. It cannot be used for calculations since it is a string value. TO_DATE (x [, date_format]) Converts a valid Numeric and Character values to a Date value. Date is formatted to the format specified by 'date_format'. NVL (x, y) If 'x' is NULL, replace it with 'y'. 'x' and 'y'must be of the same datatype. DECODE (a, b, c, d, e, default_value) Checks the value of 'a', if a = b, then returns'c'. If a = d, then returns 'e'. Else, returnsdefault_value. The below table provides the examples for the above functions Function Name Examples Return Value TO_CHAR () TO_CHAR (3000, '$9999') TO_CHAR (SYSDATE, 'Day, Month YYYY') $3000 Monday, June 2008 TO_DATE () TO_DATE ('01-Jun-08') 01-Jun-08 NVL () NVL (null, 1) 1