The MySQL REPLACE is one of the String Function, which is used to replace the existing string expression with new string value. In this article we will show you, How to write String REPLACE in MySQL with example.
Syntax of a MySQL String Replace
The basic syntax of the string Replace in MySQL is as shown below:
SELECT REPLACE (Expression, Change_String, Replace_String) FROM Source
- Expression: Please specify the valid expression on which you want to perform search. Replace function will replace portion of a string present here.
- Change_String: Specify any valid string (or character) you want to change. Whatever you place here, REPLACE Function will be replace it by Replace_String
- Replace_String: Please specify the valid new string, or charter tat you want to insert into String_Expression.
For this, We are going to use the below shown data
MySQL Replace Example 1
The Replace Function in MySQL is used to replace the characters in original string with specified expression. The following query will show multiple ways to use this String replace function.
-- MySQL String Replace Function Example SELECT REPLACE('Tutorial Gateway', 'Tutorial', 'MySQL'); SELECT REPLACE('www.tutorialgateway.org', 'org', 'net'); -- Let me Try with Integer values SELECT REPLACE('123456', 34, 75); SELECT REPLACE(123456, 234, 379); -- Testing NULLS SELECT REPLACE('Tutorial', 'Tu', NULL);
MySQL Replace Function Example 2
In this example, we implement the string replace function on different columns present in the employee table. Following statement will replace the string Developer in Department Name column with Engineer, and com in Email with net
-- MySQL String REPLACE Example USE mysqltutorial; SELECT FirstName, LastName, DepartmentName, REPLACE(DepartmentName, 'Developer', 'Engineer') AS `Replaced Dept`, Email, REPLACE(Email, 'com', 'net') AS `Replaced Email` FROM employe;
From the below screenshot you can see the replaced strings
Thank You for Visiting Our Blog