All Products
Search
Document Center

MaxCompute:REPLACE

Last Updated:Aug 16, 2023

Replaces a substring in String A that matches String B with another substring. This function is an additional function of MaxCompute V2.0.

Syntax

string replace(string <str>, string <old>, string <new>)

Parameters

  • str: required. A value of the STRING type. This parameter specifies the string that you want to replace. If the input value can be converted into a value of the STRING type, the input value is implicitly converted into a value of the STRING type before calculation.

  • old: required. This parameter specifies the string that you use for comparison.

  • new: required. This parameter specifies the string that you use to replace the original string.

Return value

If a part of the string specified by str exactly matches the string specified by old, this part of the string is replaced by the string specified by new, and the complete string after the replacement is returned. If no part of the string specified by str matches the string specified by old, the original string specified by str is returned. A value of the STRING type is returned. If an input parameter is set to null, null is returned.

Examples

  • Example 1: Replace the part of the string ababab that is exactly the same as the string abab with the string 12. Sample statement:

    -- The return value is 12ab. 
    select replace('ababab','abab','12');
  • Example 2: An input parameter is set to null. Sample statement:

    -- 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.