MySQL REPLACE() Function

The REPLACE() function in MySQL is a powerful string manipulation tool that allows you to substitute all occurrences of a specific substring within a larger string.



REPLACE(): Definition and Usage

This function replaces all instances of a search string with a replacement string within the original string. Keep in mind that the replacement is case-sensitive; "SQL" will not be replaced if you search for "sql".

Syntax

Syntax

REPLACE(string, from_string, new_string)
      

Parameter Values

Parameter Description
string The original string where you want to perform the replacement. This is required.
from_string The substring you want to replace. This is required.
new_string The substring that will replace all occurrences of from_string. This is required.

Examples

Replacing "SQL" with "HTML"

This demonstrates a simple replacement of one substring with another.

Syntax

SELECT REPLACE("SQL Tutorial", "SQL", "HTML");
      
Output

HTML Tutorial
      

Replacing "X" with "M" (Case-Sensitive)

This illustrates the case-sensitive nature of REPLACE(). Notice that only the uppercase "X" is replaced.

Syntax

SELECT REPLACE("XYZ FGH XYZ", "X", "M");
      
Output

MYZ FGH MYZ
      

More Case-Sensitive Examples

These additional examples further highlight the case-sensitivity. "X" is not replaced with "m" because they are different cases.

Syntax

SELECT REPLACE("XYZ FGH XYZ", "X", "m"); --Example 1
SELECT REPLACE("XYZ FGH XYZ", "x", "m"); --Example 2
      
Output

XYZ FGH XYZ --Example 1
XYZ FGH XYZ --Example 2