Purpose
The REPLACE() function replaces all occurrences of a substring in a string. This function searches for all instances of from_str in str and replaces them with to_str. If no match is found for from_str in str, the original string remains unchanged.
If any parameter in
REPLACE()isNULL, the result isNULL.The
REPLACE()function is case-sensitive, meaning it considers the case of the strings. If you want to perform a case-insensitive replacement, you can useREPLACE(UPPER(str), UPPER(from_str), to_str)orREPLACE(LOWER(str), LOWER(from_str), to_str)to ensure the replacement is performed without considering the case.The
REPLACE()function is multibyte-safe.
Syntax
REPLACE(str, from_str, to_str)
Parameters
Parameter |
Required/Optional |
Description |
|---|---|---|
str |
Required | The original string to be modified. |
from_str |
Required | The substring to be replaced. |
to_str |
Required | The new string after replacement. |
Examples
- Example 1: The following query example uses the
REPLACEfunction to replace all occurrences of the substringabc.in the original string withwww.
obclient> SELECT REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'www');
The result is as follows:
+---------------------------------------------------------+
| REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'www') |
+---------------------------------------------------------+
| wwwefg.gpg.nowdew.wwwdwwwe |
+---------------------------------------------------------+
1 row in set
- Example 2: The following query example uses the
REPLACEfunction to replace the substringNULLin the original string withwww. Since the substring to be replaced isNULL, the result of the entire expression will beNULL.
obclient> SELECT REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', NULL, 'www');
The result is as follows:
+-------------------------------------------------------+
| REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', NULL, 'www') |
+-------------------------------------------------------+
| NULL |
+-------------------------------------------------------+
1 row in set
- Example 3: The following query example uses the
REPLACEfunction to replace the substringabc.in the original string withWWW. Since theREPLACE()function is case-sensitive, the result of the entire expression will beWWWefg.gpg.nowdew.WWWdWWWe.
obclient> SELECT REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'WWW');
The result is as follows:
+---------------------------------------------------------+
| REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'WWW') |
+---------------------------------------------------------+
| WWWefg.gpg.nowdew.WWWdWWWe |
+---------------------------------------------------------+
1 row in set
- Example 4: In the following query example,
Hello Worldis converted to uppercaseHELLO WORLD, andworldis also converted to uppercase. Therefore, the query will return the resultHELLO Universe, as it replaces the uppercaseWORLDwithUniverse.
obclient> SELECT REPLACE(UPPER('Hello World'), UPPER('world'), 'Universe');
The result is as follows:
+-----------------------------------------------------------+
| REPLACE(UPPER('Hello World'), UPPER('world'), 'Universe') |
+-----------------------------------------------------------+
| HELLO Universe |
+-----------------------------------------------------------+
1 row in set
MySQL compatibility
The behavior of the REPLACE() function varies across different versions: In MySQL 5.7, when the second parameter is an empty string ('') and the third parameter is NULL, the result will be the first parameter. In MySQL 8.0, the result will be NULL. OceanBase Database 4.2.1 and earlier versions are compatible with this behavior of MySQL 8.0.
