REPLACE function

REPLACE is a string manipulation function that manipulates all string data types (BIT, BLOB, and CHARACTER), and replaces parts of a string with supplied substrings.

SYNTAX

REPLACE returns a string consisting of the source string, with each occurrence of the search string replaced by the replace string. The parameter strings can be of the CHARACTER, BLOB, or BIT data types, but all three must be of the same type.

If any parameter is NULL, the result is NULL.

The search process is single pass from the left and disregards characters that have already been matched. The following examples give the results shown:
      REPLACE('ABCDABCDABCDA', 'A', 'AA')             
      -- RESULT = AABCDAABCDAABCDAA
      REPLACE('AAAABCDEFGHAAAABCDEFGH', 'AA', 'XYZ')  
      -- RESULT = XYZXYZBCDEFGHXYZXYZBCDEFGH
      REPLACE('AAAAABCDEFGHAAAABCDEFGH', 'AA', 'XYZ') 
      -- RESULT = XYZXYZABCDEFGHXYZXYZBCDEFGH

The first example shows that replacement is single pass. Each occurrence of A is replaced by AA but these are not then expanded further.

The second example shows that characters once matched are not considered further. The first AA pair is matched, replaced and disregarded. The second and third As are not matched.

The third example shows that matching is from the left. The first four As are matched as two pairs and replaced. The fifth A is not matched.

If you do not specify the replace string expression, the replace string defaults to an empty string and the behavior of the function is to delete all occurrences of the search string from the result.

Related concepts
ESQL overview
Related tasks
Developing ESQL
Related reference
Syntax diagrams: available types
ESQL string manipulation functions