>>-SUBSTRING--(-------------------------------------------------> >--+-expression--FROM--start--+-------------+--USING--+-CODEUNITS16-+-+->< | '-FOR--length-' +-CODEUNITS32-+ | | '-OCTETS------' | '-expression--,--start--+-----------+--,--+-CODEUNITS16-+--)-------' '-,--length-' +-CODEUNITS32-+ '-OCTETS------'
The schema is SYSIBM.
The SUBSTRING function returns a substring of a string.
A substring of expression is zero or more contiguous string units of expression.
If expression is a fixed-length string, omission of length is an implicit specification of CHARACTER_LENGTH(expression USING string-unit) - start + 1, which is the number of string units (CODEUNITS16, CODEUNITS32, or OCTETS) from start to the last position of expression. If expression is a varying-length string, omission of length is an implicit specification of zero or CHARACTER_LENGTH(expression USING string-unit) - start + 1, whichever is greater. If the desired length is zero, the result is the empty string.
If the value is not of type INTEGER, it is implicitly cast to INTEGER before evaluating the function. The value must be greater than or equal to zero. If a value greater than n is specified, where n is the (length attribute of expression) - start + 1, then n is used as the length of the resulting substring. The value is expressed in the units that are explicitly specified. If OCTETS is specified, and expression is graphic data, the value must be an even number (SQLSTATE 428GC).
If a string unit is specified as CODEUNITS16 or CODEUNITS32, and expression is a binary string or bit data, an error is returned (SQLSTATE 428GC). If a string unit is specified as OCTETS and expression is a binary string, an error is returned (SQLSTATE 42815).
For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see "String units in built-in functions" in "Character strings".
When the SUBSTRING function is invoked using OCTETS, and the source-string is encoded in a code page that requires more than one byte per code point (mixed or MBCS), the SUBSTRING operation might split a multi-byte code point and the resulting substring might begin or end with a partial code point. If this occurs, the function replaces the bytes of leading or trailing partial code points with blanks in a way that does not change the byte length of the result. (See a related example below.)
The length attribute of the result is equal to the length attribute of expression. If any argument of the function can be null, the result can be null; if any argument is null, the result is the null value. The result is not padded with any character. If expression has actual length 0, the result also has actual length 0.
Examples:
Function ... Returns ...
------------------------------------ ------------------------------------
SUBSTRING(FIRSTNAME,1,2,CODEUNITS32) 'Jü' -- x'4AC3BC'
SUBSTRING(FIRSTNAME,1,2,CODEUNITS16) 'Jü' -- x'4AC3BC'
SUBSTRING(FIRSTNAME,1,2,OCTETS) 'J ' -- x'4A20' (a truncated string)
SUBSTRING(FIRSTNAME,8,CODEUNITS16) a zero-length string
SUBSTRING(FIRSTNAME,8,4,OCTETS) a zero-length string
SUBSTRING(UTF8_VAR, 2, 5, OCTETS)
Three
blank bytes precede the 'N', and one blank byte follows the 'N'.