Purpose
REPLACE() replaces a substring in a string. The function locates all occurrences of the from_str substring in the str string and replaces them with the value of to_str. If the function does not find any occurrences of from_str in str, the string remains unchanged.
REPLACE()returnsNULLwhen any input parameter isNULL.REPLACE()is case-sensitive, and therefore it takes into account the letter case of strings. If you want to make a case-insensitive replacement, useREPLACE(UPPER(str), UPPER(from_str), to_str)orREPLACE(LOWER(str), LOWER(from_str), to_str).REPLACE()is multi-byte safe.
Syntax
REPLACE(str, from_str, to_str)
Parameters
| Parameter | Required | Description |
|---|---|---|
str |
Yes | The original string in which a substring is to be replaced. |
from_str |
Yes | The substring to be replaced. |
to_str |
Yes | The new substring. |
Examples
Example 1: Use
REPLACE()to replace allabc.in the original string withwww.obclient> SELECT REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'www');The return result is as follows:
+---------------------------------------------------------+ | REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'www') | +---------------------------------------------------------+ | wwwefg.gpg.nowdew.wwwdwwwe | +---------------------------------------------------------+ 1 row in setExample 2: Use
REPLACE()to replaceNULLin the original string withwww. Since the substring to be replaced isNULL, the return result isNULL.obclient> SELECT REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', NULL, 'www');The return result is as follows:
+-------------------------------------------------------+ | REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', NULL, 'www') | +-------------------------------------------------------+ | NULL | +-------------------------------------------------------+ 1 row in setExample 3: Use
REPLACE()to replaceabc.in the original string withWWW. SinceREPLACE()is case-sensitive, the return result isWWWefg.gpg.nowdew.WWWdWWWe.obclient> SELECT REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'WWW');The return result is as follows:
+---------------------------------------------------------+ | REPLACE('abc.efg.gpg.nowdew.abc.dabc.e', 'abc.', 'WWW') | +---------------------------------------------------------+ | WWWefg.gpg.nowdew.WWWdWWWe | +---------------------------------------------------------+ 1 row in setExample 4: Convert
Hello WorldtoHELLO WORLD, andworldtoWORLD. UseREPLACE()to replaceWORLDwithUniverse. The return result isHELLO Universe.obclient> SELECT REPLACE(UPPER('Hello World'), UPPER('world'), 'Universe');The return result is as follows:
+-----------------------------------------------------------+ | REPLACE(UPPER('Hello World'), UPPER('world'), 'Universe') | +-----------------------------------------------------------+ | HELLO Universe | +-----------------------------------------------------------+ 1 row in set
Compatibility with MySQL
REPLACE() in MySQL behaves differently in different versions. In MySQL 5.7, when the second parameter is an empty string ('') and the third parameter is NULL, the function returns the first parameter, while in MySQL 8.0, the return result in this case is NULL. OceanBase Database V4.2.1 and earlier versions are compatible with the function behavior in MySQL 8.0.