MySQL REPLACE Function

The MySQL REPLACE function is one of the String Function, which is useful to replace the existing string expression with a new string value. The basic syntax of 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 the search. Replace function replace a 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 replace it by Replace_String
  • Replace_String: Please specify the valid new string or charter that you want to insert into String_Expression.

For this MySQL String REPLACE demonstration, We are going to use the below shown data

MySQL String Replace Function Example

MySQL Replace String Example 1

The String Replace Function is used to replace the characters in the original string with the specified expression. The below query shows various 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 String Function Example 1

Replace String Example 2

In this example, we implement the string replace function on different columns present in the employee table. The following String Function statement replaces 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 MySQL screenshot you can see the replaced strings

MySQL Replace String Function Example 2