DB2 Scalar functions - REPLACE
Replaces all occurrences of search-string in source-string with replace-string.
The schema is SYSIBM. The SYSFUN version of the REPLACE function continues to be available but it is not sensitive to the database collation.
If the search string is not found in the source string, the search string is returned unchanged. If the Unicode database is defined with a locale-sensitive UCA-based collation and none of the source-string, search-string, or replace-string arguments are defined as FOR BIT DATA or as a binary string, a linguistically correct search is done. Otherwise, the search is done using a binary comparison with no special consideration for multi-byte characters.
All three arguments must have compatible data types.
The data type of the result depends on the data type of the arguments:
In a Unicode database, if both character strings and graphic strings are used as arguments, then the result data type is based on the string type of the source-string argument. If the source-string argument is a character string type, then the result data type is VARCHAR. If the source-string argument is a graphic string type, then the result data type is VARGRAPHIC.
The string unit of the result is the string unit of source-string. If any argument is defined as FOR BIT DATA, the other arguments cannot be defined with string units of CODEUNITS32.
The string units of the result is the same as the string units of source-string. The length attribute of the result depends on the arguments:
If the result is a character string, the length attribute of the result must not exceed the maximum length of the VARCHAR data type in the string units of the result. If the result is a graphic string, the length attribute of the result must not exceed the maximum length of the VARGRAPHIC data type in the string units of the result.
The actual length of the result is the actual length of source-string plus the number of occurrences of search-string that exist in source-string multiplied by the actual length of replace-string minus the actual length of search-string. If the actual length of the result would exceed the length attribute of the result or would exceed 1,048,576 bytes, an error is returned (SQLSTATE 22001).
If the actual length of the replace-string exceeds the maximum for the return data type, an error is returned. If any argument can be null, the result can be null; if any argument is null, the result is the null value.
VALUES CHAR (REPLACE ('DINING', 'N', 'VID'), 10)
VALUES REPLACE ('The quick brown fox', 'QUICK', 'LARGE')