DB2 Scalar functions - REGEXP_REPLACE
The REGEXP_REPLACE scalar function returns a modified version of the source string where occurrences of the regular expression pattern found in the source string are replaced with the specified replacement string.
The schema is SYSIBM.
The content of the replacement string can include references to capture group text from the search to use in the replacement text. These references are of the form '$n' or '\n', where n is the number of the capture group and 0 represents the entire string that matches the pattern. The value for n must be in the range 0-9 and not greater than the number of capture groups in the pattern (SQLSTATE 2201V). For example, either '$2' or '\2' can be used to refer to the content found in the source string for the second capture group that is specified in the pattern expression. If the pattern expression must include a literal reference to a '$' or '\' character, that character must be preceded with an '/' character as an escape character ('\$' or '\\').
If the string unit is specified as CODEUNITS16 or OCTETS, and if the string unit of the source string is CODEUNITS32, an error is returned (SQLSTATE 428GC).
For more information, see "String units in built-in functions" in Character strings.
The result of the function is a string. If there are no occurrences of the pattern to be replaced and no argument is null, the original string is returned. The data type of the string is the same data type as the source string, except for CHAR, which becomes VARCHAR; and VARGRAPHIC, which becomes GRAPHIC.
The length attribute of the result data type is determined based on the length attributes of the source string and the replacement string by using the following calculation:
MIN(MaxTypeLen, LAS+(LAS+1)*LAR)
If any argument of the REGEXP_REPLACE function can be null, the result can be null. If any argument is null, the result is the null value.
Replace the second occurrence of the pattern 'R.d' with 'Orange' using a case sensitive search.
SELECT REGEXP_REPLACE('Red Yellow RED Blue Red Green Blue', 'R.d','Orange',1,2,'c') FROM SYSIBM.SYSDUMMY1