What is the usage of the replace function in Oracle?
The REPLACE function in Oracle is used to replace a specified portion of a string. Its syntax is as follows:
Would you please replace the original string with this new one?
Description of parameters:
- source string: the original string that needs to be replaced.
- search_string: the part of the string that needs to be replaced.
- replacement_string: the string used for replacement.
The function returns the replaced result string.
Example of usage:
- Replace a single character.
- The output is “Helli”.
- Replace multiple characters:
- The output will be “Heyyo” when replacing ‘l’ with ‘y’ in the word ‘Hello’.
- Replace a substring in a string.
- The output of the following code will be “Helli, Wirld!”
- Using the REPLACE function multiple times in a row:
- The result of the output is “Heyyi”
Important Note:
- The REPLACE function is case-sensitive, if you need to be case-insensitive, you can use the LOWER or UPPER function for conversion.
- If the replacement_string parameter is empty, the part matched by the search_string will be deleted.
- If the search_string parameter is empty, the original string will be returned.
- The REPLACE function only replaces the first matching part, if you need to replace all matching parts, you can use the REGEXP_REPLACE function.