EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • 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
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MySQL Tutorial MySQL REPLACE
 

MySQL REPLACE

Aanchal Sharma
Article byAanchal Sharma
EDUCBA
Reviewed byRavi Rathore

Updated May 16, 2023

MySQL REPLACE

 

 

Introduction to MySQL REPLACE

The MySQL REPLACE function is one of the string functions used to replace all existences of a substring within the main string to result in a new substring.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

The MySQL REPLACE function includes three parameters. The first parameter represents the main string where the replacement will occur. The other two parameters specify the substring to be replaced within the first string and the new substring for replacement.

This task in MySQL makes a case-sensitive function implementation while the string replacement process.

Hence, MySQL allows us to replace an old string with the new one in a column of the database table to perform any handy search and replace the needed one with the existing record value.

Syntax

The MySQL REPLACE function is used with the following syntax:

REPLACE(Original_String, Substring1, Subtring2)

The terms given in the above syntax are explained below:

  • Original_String: This term denotes the main string in which the new one replaces the old string.
  • Substring1: This is the required occurrence of the substring to be replaced present in the original string.
  • Substring2: This is the required substring with the help of which a new substring can be replaced from the old one.

How does the REPLACE function work in MySQL?

First, Let us check a simple example to learn the implementation of REPLACEfunction in MySQL. The query is written as follows, where the original string is “VBN” and the substring “B” within “VBN” will be replaced by the new string “X”:

SELECT REPLACE("VBN HJB", "B", "X");

Result:

MySQL REPLACE output 1

As per the above query, the valid expression is specified to replace the old string with the new one in the main string. The function executes to replace all the occurrences of the second argument in a specified string using the desired new one.

Thus, Using the REPLACE function in MySQL, we can effectively handle spelling mistakes in columns and perform searches for words that may not be accurate or valid. We can update the records by replacing specific characters with appropriate ones to obtain the desired results.

In this way, for a column value replacement, let us take the statement as follows:

UPDATE Person SET Person_Address = REPLACE(Person_Address,'Jaiput','Jaipur');

This above helps to find all the occurrences of a spelling error in the column of the address of the table person and updates it with the correct one. For the function, the first parameter defines the specified column name without quotes, and the other two denote the substrings, which are responsible for replacing each other to produce a new string.

We should know that if we apply quotes with the field column in the function like ‘Person_Address’, then. As a result, the values of that column will be updated by this ‘Person_Address’. This will cause a sudden data loss of that column in the table.

Note that MySQL REPLACE function will make logical sense only if there exists a Primary Key or a Unique key in the table database so that the function can determine a new row without going through duplicity to make a replacement using indexes of the table; otherwise, it will be corresponding to an INSERT statement.

Also, learn that this function does not upkeep regular expression. If we want to substitute any text string using a specific pattern, we need to use a user-defined MySQL function, i.e., UDF from an external library.

Examples of MySQL REPLACE

We will execute the below queries, which show various ways to use this MySQL REPLACE string function in a database table:

1. Replace the substring ‘mysql.com’ with ‘mysql.org’

Let us take a basic example of using MySQL REPLACE function:

Code:

SELECT REPLACE("mysql.com", "com", "org");

Output:

MySQL REPLACE output 2

2. Correcting invalid String characters within a word

We are replacing the word ‘Learnint’, correcting a spelling error with the ‘g’ substring, and removing it.

Code:

SELECT REPLACE("Learnint","t", "g");

Output:

MySQL REPLACE output 3

3. Replace the substring containing numbers

Let us suppose the below query to replace a specific number string:

Code:

SELECT REPLACE("477", "7", "9");

Output:

MySQL REPLACE output 4

4. Replace string using the Column name of a table and SELECT Statement

Considering a sample table named Books, we will use the MySQL REPLACE function in the query to show the string exchange of column values having the substring within it.

Code:

select * from Books;

MySQL REPLACE output 5

For example, the query is executed as below to display the result:

Code:

SELECT BookID,BookName,Language, REPLACE(Language, 'English', 'Eng') AS 'Replaced Language' FROM books;

Output:

output 6

5. Replace string Example With SELECT & WHERE statement

We are now using the MySQL REPLACE function for a column of the Books table using a SELECT statement and will interchange the book name column string having a substring within the value to a new substring applied to the search. Table Books:

Code:

select * from Books;

output 7

The query is below:

Code:

SELECT BOOKID, BOOKNAME, REPLACE(BOOKNAME,'Science','Sci') FROM Books2 WHERE BOOKNAME = 'Nuclear Science';

Output:

output 8

6. MySQL REPLACE function with the UPDATE statement

Following is the syntax to apply MySQL REPLACE function together with an UPDATE query:

Code:

UPDATE TableName SET ColumnName = REPLACE(ColumnName, Substring1, Substring2) WHERE CondExpr;//condExprs: Conditional expression

For example, taking the sample table Person, we perform the below query:

Code:

UPDATE Books2 SET BOOKNAME = REPLACE(BOOKNAME,'Networking','Computer Network');
select * from Books2;

Output:

output 9

Code:

update Books set PRICE = '2000' where BOOKID = 107;
select * from Books;

Output:

output 10

7. Case Sensitive Error

If we execute the below query in MySQL, then the result will be as follows:

Code:

SELECT REPLACE('APPLE', 'e', 'g');

Output:

output 11

In this way, The MySQL REPLACE function supports a case-sensitive search for a string that needs to be replaced, resulting in a new value. So, we must specify valid string expressions on which we want to perform the search and replacement. If not, the result will be the same as the original one.

Conclusion

This article taught us about the MySQL REPLACE function and how to exchange an old string with a new one.

Thus, the MySQL REPLACE function helps to substitute text in a table column for tasks such as interchanging the obsolete link, fixing any spelling error in the database records, etc.

Recommended Articles

We hope that this EDUCBA information on “MySQL REPLACE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL between
  2. SELECT in MySQL
  3. Insert in MySQL
  4. MySQL Operators

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - 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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW