DB2 Scalar functions - OVERLAY
The OVERLAY function returns a string in which, beginning at start in source-string, length of the specified code units have been deleted and insert-string has been inserted.
The schema is SYSIBM.
Not specifying length is equivalent to specifying a value of 1, except when OCTETS is specified and the result is graphic data, in which case, not specifying length is equivalent to specifying a value of 2.
CODEUNITS16 specifies that start and length are expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that start and length are expressed in 32-bit UTF-32 code units. OCTETS specifies that start and length are expressed in bytes.
If a string unit argument is not specified and both source-string and insert-string are either a character string that is not FOR BIT DATA or is a graphic string, the default is CODEUNITS32. Otherwise, the default is OCTETS.
For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see String units in built-in functions in Character strings.
String units in built-in functions
Character strings
The data type of the result depends on the data types of source-string and insert-string, as shown in the following tables of supported type combinations. The string unit of the result is the string unit of source-string. If either source-string or insert-string is defined as FOR BIT DATA the other argument cannot be defined with string units of CODEUNITS32. The second table applies to Unicode databases only.
Note: If the data types for source-string and insert-string are a combination of binary and FOR BIT DATA string, the argument that is not a binary data type is handled as if it was cast to the corresponding binary data type.
A source-string can have a length of 0; in this case, start must be 1 (as implied by the bounds provided in the description for start), and the result of the function is a copy of the insert-string.
An insert-string can also have a length of 0. This has the effect of deleting the code units identified by start and length from the source-string.
The length attribute of the result is the length attribute of source-string plus the length attribute of insert-string when the string units of the source-string and insert-string are the same or the result string units is CODEUNITS32. Special cases are listed in the following table.
The actual length of the result depends on the actual length of source-string, the actual length of the deleted string, the actual length of the insert-string, and string units used for the start and length arguments. For example, if the string arguments are character strings in OCTETS and the OCTETS is used as the fourth argument, the actual length of the result is A1 - MIN((A1 - V2 + 1), V3) + A4, where:
If any argument can be null, the result can be null; if any argument is null, the result is the null value.
SELECT OVERLAY('INSERTING','IS',4,2,OCTETS), OVERLAY('INSERTING','IS',4,0,OCTETS), OVERLAY('INSERTING','',4,2,OCTETS) FROM SYSIBM.SYSDUMMY1
SELECT OVERLAY('INSERTING','XX',1,0,CODEUNITS16)), OVERLAY('INSERTING','XX',1,1,CODEUNITS16)), OVERLAY('INSERTING','XX',1,2,CODEUNITS16)), OVERLAY('INSERTING','XX',1,3,CODEUNITS16)) FROM SYSIBM.SYSDUMMY1
SELECT OVERLAY('ABCABC','XX',7,0,CODEUNITS16)) FROM SYSIBM.SYSDUMMY1
SELECT OVERLAY('Hegelstraße','ss',10,1,CODEUNITS16)) FROM SYSIBM.SYSDUMMY1
Assume that the variables UTF8_VAR and UTF16_VAR contain the UTF-8 and the UTF-16BE representations of the string, respectively. Use the OVERLAY function to insert a 'C' into the Unicode string '&N~AB'.
SELECT OVERLAY(UTF8_VAR, 'C', 1, CODEUNITS16), OVERLAY(UTF8_VAR, 'C', 1, CODEUNITS32), OVERLAY(UTF8_VAR, 'C', 1, OCTETS) FROM SYSIBM.SYSDUMMY1
SELECT OVERLAY(UTF8_VAR, 'C', 5, CODEUNITS16), OVERLAY(UTF8_VAR, 'C', 5, CODEUNITS32), OVERLAY(UTF8_VAR, 'C', 5, OCTETS) FROM SYSIBM.SYSDUMMY1
SELECT OVERLAY(UTF16_VAR, 'C', 1, CODEUNITS16), OVERLAY(UTF16_VAR, 'C', 1, CODEUNITS32) FROM SYSIBM.SYSDUMMY1
SELECT OVERLAY(UTF16_VAR, 'C', 5, CODEUNITS16), OVERLAY(UTF16_VAR, 'C', 5, CODEUNITS32) FROM SYSIBM.SYSDUMMY1