EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Oracle REPLACE()

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » Oracle Tutorial » Oracle REPLACE()

Oracle REPLACE()

Introduction to Oracle REPLACE()

An Oracle REPLACE Function is used to replace the string with a given string as the name suggests. It accepts a search string and replaces it with another given string but pattern-wise.

  • It returns CHAR with every replaced with replacement string for the search string.
  • If the first argument is not LOB, it returns VARCHAR2.
  • If the first argument is LOB, it returns CLOB.
  • It returns every occurrence of the search string replaced by the replacement string.
  • If the replacement string is omitted or NULL, all occurrences of the search string will be removed.
  • It substitutes one string for another as well as removes character strings.

Syntax

REPLACE(CHAR, Search_str, Replace_str)

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Explanation:

  • CHAR: It can be a column name or string to replace a sequence of characters with another set of characters.
  • Search_str: It is a string that will be searched for in CHAR.
  • Replace_str: It is a string that will replace the Search_str in CHAR. It is optional. If Replace_str has omitted or NULL all occurrences of Search_str will be removed.
Parameter Data Type
CHAR CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB
Search_str CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB
Replace_str CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB

How REPLACE() function works in Oracle?

As REPLACE function syntax shows it accepts a maximum of three arguments. They are CHAR, Search_str, and Replace_str. So the function takes the Search_str and searches it in CHAR if it finds any match or occurrences then it replaces it with Replace_str and returns the updated CHAR values. If it does not find any match then return the existing CHAR values. Here Replace_str is an optional parameter. It can be omitted or NULL in that case it removes the matching string. But the Search_str cannot be omitted else it returns “missing expression” error.

Examples to Implement REPLACE( ) Function

Below are some examples mentioned:

In this section we’ll see the implementation of Oracle REPLACE Function and its behavior. For that, we will use the below sample table (Employee) with 14 records to understand the Oracle REPLACE( ) function behavior.

SELECT * Employee;

Output:

employee table

Example #1

REPLACE( ) Function with all three parameter

Code:

SELECT Designation, REPLACE(Designation,'SOFTWARE','Sr.') Replaced FROM        Employee WHERE Designation='SOFTWARE ENGINEER';

Output:

three parameters

Explanation: In the above example, REPLACE function replacing ‘SOFTWARE ENGINEER’ with ‘Sr. ENGINEER’. Because Search_str (‘‘SOFTWARE’) searching for the occurrence in the Designation column and where ever it finds, replaced it with the given Replace_str (‘Sr.’) and returns the updated result.

Code:

SELECT 'JACK AND JUE' OrgStr, REPLACE('JACK AND JUE','J','BL') Replace FROM DUAL;

Output:

Oracle REPLACE()3

Explanation: In the above example ‘J’ is being replaced by ‘BL’. Here ‘J’ is a single character but ‘BL’ is double because REPLACE function operates as a string not a character. That’s why ‘J’ is being replaced by ‘BL’.

Popular Course in this category
Sale
Oracle Training (14 Courses, 8+ Projects)14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (9,563 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects

Example #2

REPLACE( ) Function without Replace_str

Code:

SELECT Designation, REPLACE(Designation,'SOFTWARE') Replaced FROM Employee WHERE Designation='SOFTWARE ENGINEER';

Output:

without replace str

Explanation: In the above example, REPLACE function replacing ‘SOFTWARE ENGINEER’ with ‘ENGINEER’. Because Search_str (‘‘SOFTWARE’) searching for the occurrence in the Designation column and where ever it finds, it gets removed because Replace_str is omitted.

Example #3

REPLACE( ) Function without Search_str

Code:

SELECT Designation, REPLACE(Designation, ,'Sr.') Replaced FROM Employee WHERE Designation='SOFTWARE ENGINEER';

Output:

Oracle REPLACE()5

Explanation: In the above example, the Search_str parameter of the REPLACE function is omitted. And because it is a mandatory parameter, it returns a “missing expression” error.

Example #4

REPLACE( ) function for finding number of Search_str occurrences

Code:

SELECT 'JACK AND JUE' OrgStr, LENGTH('JACK AND JUE')-LENGTH(REPLACE('JACK AND JUE', 'J')) "N J's Found" FROM DUAL;

Output:

Oracle REPLACE()6

Explanation: In the above example, REPLACE function playing a vital role with LENGTH function to find out the number of occurrences of Search_str.

LENGTH(REPLACE(‘JACK AND JUE’, ‘J’)) : In this part REPLACE function removes ‘J’ because Replace_str is omitted and then LENGTH function calculates the length of the CHAR parameter (‘JACK AND JUE’) that becomes 10.

LENGTH(‘JACK AND JUE’): In this part LENGTH function calculate the length of the CHAR parameter (‘JACK AND JUE’) that is 12. And then Subtract function (-) subtracts it 12-10 and returns the number of Search_str occurrences.

Tips:

  1. The functionality of the REPLACE function is similar to the TRANSLATE function. Translate single character, one to one substitution while REPLACE provides string
  2. REPLACE function can be used as a nested REPLACE function.

Code:

SELECT 'JACK AND JUE' OrgStr, REPLACE(REPLACE('JACK AND JUE','J', 'BL'), ’BL’, ’TR’) Replace FROM DUAL;

Output:

replace function

Example #3

REPLACE function can be applied on the NUMBER data type column.

Code:

SELECT Salary, REPLACE(Salary, 30, 40) Sal_Replace FROM Employee;

Output:

Number data type

Conclusion

Oracle REPLACE( ) function is very useful function for string substitution. This function can be used to calculate the number of Search_str occurrences. This is the simplest way to replace or substitute the string.

Recommended Articles

This is a guide to Oracle REPLACE(). Here we discuss introduction to Oracle REPLACE() along with syntax, how does it work and examples for better understanding. You can also go through our other related articles to learn more –

  1. Oracle UNIQUE Constraint
  2. FETCH in Oracle
  3. Oracle Subquery
  4. Oracle COMMIT 

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Oracle Course Training Learn More