Updated May 16, 2023
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.
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.
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");
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:
SELECT REPLACE("mysql.com", "com", "org");
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.
SELECT REPLACE("Learnint","t", "g");
3. Replace the substring containing numbers
Let us suppose the below query to replace a specific number string:
SELECT REPLACE("477", "7", "9");
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.
select * from Books;
For example, the query is executed as below to display the result:
SELECT BookID,BookName,Language, REPLACE(Language, 'English', 'Eng') AS 'Replaced Language' FROM books;
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:
select * from Books;
The query is below:
SELECT BOOKID, BOOKNAME, REPLACE(BOOKNAME,'Science','Sci') FROM Books2 WHERE BOOKNAME = 'Nuclear Science';
6. MySQL REPLACE function with the UPDATE statement
Following is the syntax to apply MySQL REPLACE function together with an UPDATE query:
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:
UPDATE Books2 SET BOOKNAME = REPLACE(BOOKNAME,'Networking','Computer Network'); select * from Books2;
update Books set PRICE = '2000' where BOOKID = 107; select * from Books;
7. Case Sensitive Error
If we execute the below query in MySQL, then the result will be as follows:
SELECT REPLACE('APPLE', 'e', 'g');
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.
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.
We hope that this EDUCBA information on “MySQL REPLACE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.