EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle REPLACE()

Oracle REPLACE()

Priya Pedamkar
Article byPriya Pedamkar

Updated February 27, 2023

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.

ADVERTISEMENT
Popular Course in this category
ORACLE DBA Database Management System Course Bundle - 2 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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)

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’.

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 
ADVERTISEMENT
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Let’s Get Started

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

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

Forgot Password?

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

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW