The INSERT function returns a string where, beginning at start in source-string, length of the specified code units have been deleted and insert-string has been inserted.
>>-INSERT--(--source-string-------------------------------------> >--,--start--,--length--,--insert-string------------------------> >--+-----------------------+----------------------------------->< '-,--+-CODEUNITS16-+--)-' +-CODEUNITS32-+ '-OCTETS------'
The schema is SYSIBM. The SYSFUN version of the INSERT function continues to be available.
The INSERT function is identical to the OVERLAY function, except that the length argument is mandatory.
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 the string unit is specified as CODEUNITS16 or CODEUNITS32, and the result is a binary string or bit data, an error is returned (SQLSTATE 428GC). If the string unit is specified as OCTETS, and insert-string and source-string are binary strings, an error is returned (SQLSTATE 42815). If the string unit is specified as CODEUNITS16 or OCTETS, and the string units of source-string is CODEUNITS32, an error is returned (SQLSTATE 428GC). If the string unit is specified as OCTETS, the operation is performed in the code page of the source-string. If a string unit is not explicitly specified, the string unit of the source-string determines the unit that is used. For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see "String units in built-in functions" in "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 only to Unicode databases.
source-string | insert-string | Result |
---|---|---|
CHAR or VARCHAR | CHAR or VARCHAR | VARCHAR |
GRAPHIC or VARGRAPHIC | GRAPHIC or VARGRAPHIC | VARGRAPHIC |
CLOB | CHAR, VARCHAR, or CLOB | CLOB |
DBCLOB | GRAPHIC, VARGRAPHIC, or DBCLOB | DBCLOB |
CHAR or VARCHAR | CHAR FOR BIT DATA or VARCHAR FOR BIT DATA | VARCHAR FOR BIT DATA |
CHAR FOR BIT DATA or VARCHAR FOR BIT DATA | CHAR, VARCHAR, CHAR FOR BIT DATA, or VARCHAR FOR BIT DATA | VARCHAR FOR BIT DATA |
BLOB | BLOB | BLOB |
source-string | insert-string | Result |
---|---|---|
CHAR or VARCHAR | GRAPHIC or VARGRAPHIC | VARCHAR |
GRAPHIC or VARGRAPHIC | CHAR or VARCHAR | VARGRAPHIC |
CLOB | GRAPHIC, VARGRAPHIC, or DBCLOB | CLOB |
DBCLOB | CHAR, VARCHAR, or CLOB | DBCLOB |
A source-string can have a length of 0; in this case, start must be 1 (as implied by the bounds for start described previously), 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.
source-string | insert-string | Result | |||
---|---|---|---|---|---|
Data type | String units | Data type | String units | Length attribute | String units |
Character string with length attribute A | OCTETS | Graphic string with length attribute B | CODEUNITS16 | A+3*B | OCTETS |
CODEUNITS32 | A+4*B | ||||
Character with length attribute B | CODEUNITS32 | A+4*B | |||
Graphic string with length attribute A | CODEUNITS16 | Character with length attribute B | OCTETS | A+B | CODEUNITS16 |
CODEUNITS32 | A+2*B | ||||
Graphic string with length attribute B | CODEUNITS32 | A+2*B |
If any argument can be null, the result can be null; if any argument is null, the result is the null value.
SELECT INSERT('INSERTING',4,2,'IS'),
INSERT('INSERTING',4,0,'IS'),
INSERT('INSERTING',4,2,'')
FROM SYSIBM.SYSDUMMY1
SELECT INSERT('INSERTING',1,0,'XX'),
INSERT('INSERTING',1,1,'XX'),
INSERT('INSERTING',1,2,'XX'),
INSERT('INSERTING',1,3,'XX')
FROM SYSIBM.SYSDUMMY1
SELECT INSERT('ABCABC',7,0,'XX')
FROM SYSIBM.SYSDUMMY1
SELECT INSERT('Hegelstraße',10,1,'ss',CODEUNITS16)
FROM SYSIBM.SYSDUMMY1
'&' | 'N' | '~' | 'A' | 'B' | |
---|---|---|---|---|---|
UTF-8 | X'F09D849E' | X'4E' | X'CC83' | X'41' | X'42' |
UTF-16BE | X'D834DD1E' | X'004E' | X'0303' | X'0041' | X'0042' |
SELECT INSERT(UTF8_VAR, 1, 4, 'C', CODEUNITS16),
INSERT(UTF8_VAR, 1, 4, 'C', CODEUNITS32),
INSERT(UTF8_VAR, 1, 4, 'C', OCTETS)
FROM SYSIBM.SYSDUMMY1
returns the values 'CAB',
'CB', and 'CN~AB', respectively. SELECT INSERT(UTF8_VAR, 5, 1, 'C', CODEUNITS16),
INSERT(UTF8_VAR, 5, 1, 'C', CODEUNITS32),
INSERT(UTF8_VAR, 5, 1, 'C', OCTETS)
FROM SYSIBM.SYSDUMMY1
returns the values '&N~CB',
'&N~AC', and '&C~AB', respectively. SELECT INSERT(UTF16_VAR, 1, 4, 'C', CODEUNITS16),
INSERT(UTF16_VAR, 1, 4, 'C', CODEUNITS32),
INSERT(UTF16_VAR, 1, 4, 'C', OCTETS)
FROM SYSIBM.SYSDUMMY1
returns the values 'CAB',
'CB', and 'CN~AB', respectively. SELECT INSERT(UTF16_VAR, 5, 2, 'C', CODEUNITS16),
INSERT(UTF16_VAR, 5, 1, 'C', CODEUNITS32),
INSERT(UTF16_VAR, 5, 4, 'C', OCTETS)
FROM SYSIBM.SYSDUMMY1
returns the values '&N~C',
'&N~AC', and '&CAB', respectively.