REPLACE
Description
The REPLACE function is used to replace all occurrences of a specified substring in a string with a new substring. This function replaces all matching instances of the substring in the string, performing a global replace operation.
Difference from REPLACE_EMPTY function:
REPLACE()replaces the specified substring, including empty stringsREPLACE_EMPTY()is specifically used to replace empty values or empty strings with a specified value
Syntax
REPLACE(<str>, <old>, <new>)
Parameters
| Parameter | Description |
|---|---|
<str> | The source string where replacement will occur. Type: VARCHAR |
<old> | The target substring to be replaced. If not found in str, no replacement occurs. Type: VARCHAR |
<new> | The new substring used to replace old. Type: VARCHAR |
Return Value
Returns VARCHAR type, representing the new string after replacement.
Replacement rules:
- Replaces all matching old substrings in the string
- Replacement is case-sensitive
- If old is an empty string, returns the original string (no operation performed)
- If new is an empty string, effectively deletes all matching old substrings
Special cases:
- If any parameter is NULL, returns NULL
- If str is an empty string, returns an empty string
- If old is an empty string, returns the original str (no replacement)
- If old is not found in str, returns the original str
Examples
- Basic replacement operation
SELECT REPLACE('hello world', 'world', 'universe');
+---------------------------------------------+
| REPLACE('hello world', 'world', 'universe') |
+---------------------------------------------+
| hello universe |
+---------------------------------------------+
- Replace multiple matches
SELECT REPLACE('apple apple apple', 'apple', 'orange');
+------------------------------------------------+
| REPLACE('apple apple apple', 'apple', 'orange') |
+------------------------------------------------+
| orange orange orange |
+------------------------------------------------+
- Delete substring (replace with empty string)
SELECT REPLACE('banana', 'a', '');
+---------------------------+
| REPLACE('banana', 'a', '') |
+---------------------------+
| bnn |
+---------------------------+
- NULL value handling
SELECT REPLACE(NULL, 'old', 'new'), REPLACE('test', NULL, 'new'), REPLACE('test', 'old', NULL);
+------------------------------+------------------------------+------------------------------+
| REPLACE(NULL, 'old', 'new') | REPLACE('test', NULL, 'new') | REPLACE('test', 'old', NULL) |
+------------------------------+------------------------------+------------------------------+
| NULL | NULL | NULL |
+------------------------------+------------------------------+------------------------------+
- UTF-8 character replacement
SELECT REPLACE('ṭṛì ḍḍumai test ṭṛì ḍḍumannàri', 'ṭṛì', 'replaced');
+-----------------------------------------------------------+
| REPLACE('ṭṛì ḍḍumai test ṭṛì ḍḍumannàri', 'ṭṛì', 'replaced') |
+-----------------------------------------------------------+
| replaced ḍḍumai test replaced ḍḍumannàri |
+-----------------------------------------------------------+