Introduction to MySQL REPLACE
The MySQL REPLACE function is one of the string functions, which is used to replace all existences of a substring within the main string to result in a new substring.
In this MySQL REPLACE function, three parameters are included where one denotes the main string where the replacement is to be made and the other two strings are used as the parameters to support for replacing a string within the first one with another one.
This task in MySQL makes a case-sensitive function implementation while string replacement process.
Hence, MySQL allows us to replace an old string with the new one in a column of the database table so that we can perform any handy search in the table and replace the needed one with the existing one record value.
We have the following syntax to be used for MySQL REPLACE function:
REPLACE(Original_String, Substring1, Subtring2)
The terms given in the above syntax are explained below:
- Original_String: This term denotes the main string within which the old string is replaced from the new string.
- Substring1: This is the required occurrences of the substring to be replaced present in the original string.
- Substring2: This is the required substring with the help of which new substring can be replaced from the old one.
How does the REPLACE function work in MySQL?
Let us first check a simple basic example to learn the implementation of REPLACEfunction in MySQL. The query is written as follows where the original string is VBNand the substring B within VBN will be substituted by the new string X:
SELECT REPLACE("VBN HJB", "B", "X");
As per the above query, we see that the valid expression is specified to perform replacement from old string to new 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 work to handle any spelling mistakes found in the columns or if we want to search the words that may not be correct or false and then, want to replace some characters with the effective one so that we get the updated records.
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 to replace each other with one another 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 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. This means if we want to substitute any text string using a certain pattern then, we need to use 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 for using MySQL REPLACE function:
SELECT REPLACE("mysql.com", "com", "org");
2. Correcting invalid String character within a word
We are replacing the word ‘Learnint’ and correcting it’s a spelling error with ‘g’ substring and removing‘t’.
SELECT REPLACE("Learnint","t", "g");
3. Replace the substring containing numbers
Let us suppose the below query to replace certain number string:
SELECT REPLACE("477", "7", "9");
4. Replace string using Column name of a table and SELECT Statement
Considering a sample table named Books, we will use 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 MySQL REPLACE function for a column of Books table using SELECT statement and will interchange the book name column string having a substring within the value to a new substring apply searching. 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 like query in MySQL, then the result will be as follows:
SELECT REPLACE('APPLE', 'e', 'g');
In this way, MySQL REPLACE function supports a case- sensitive search for a string to be interchanged and form a new result. So, we must use specifying valid string expression on which we want to perform the search and replacement if not then the result will be the same as the original one.
In this article, we have learned about the MySQL REPLACE function and how we use it to allow exchanging the old string with a new one.
Thus, 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.
This is a guide to MySQL REPLACE. Here we discuss How to use the MySQL REPLACE along with the query examples for better understanding. You may also have a look at the following articles to learn more –