REPLACE replaces all occurrences of a specified substring in a string with another string. This function is an additional function of MaxCompute V2.0.
Syntax
string replace(string <str>, string <old>, string <new>)
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| str | Yes | STRING | The source string to search. Compatible types are implicitly converted to STRING before the function runs. |
| old | Yes | STRING | The substring to search for. |
| new | Yes | STRING | The string to substitute for each match. |
Usage notes
-
All occurrences of
oldinstrare replaced, not just the first occurrence. -
If no part of
strmatchesold, the original string is returned unchanged. -
If any parameter is NULL, the function returns NULL.
Return value
Returns a STRING value.
Examples
Example 1: Replace a substring in a literal string
-- The return value is 12ab.
select replace('ababab', 'abab', '12');
Example 2: NULL parameter
-- The return value is null.
select replace('123abab456ab', null, 'abab');
Related functions
REPLACE is a string function. For more information about functions related to string searches and conversion, see String functions.