DB2 Scalar functions - SUBSTRING
The SUBSTRING function returns a substring of a string.
The schema is SYSIBM.
A substring of the input expression comprises zero or more contiguous string units of the input expression.
The start value can be positive, negative, or zero. If OCTETS is specified and the input expression contains graphic data, the start value must be odd (SQLSTATE 428GC).
If the input expression is:
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 string units that are explicitly specified. If OCTETS is specified, and if the input expression contains graphic data, the length must be an even number (SQLSTATE 428GC).
If string units are specified as CODEUNITS16 or CODEUNITS32, and expression is a binary string or a FOR BIT DATA string, an error is returned (SQLSTATE 428GC).
If a string units argument is not specified and expression is 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
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 in the Examples section.)
The data type of the result depends on the data type of the first argument, as shown in the following table.
The length attribute of the result is equal to the length attribute of the input 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 the input expression has an actual length of 0, the result also has an actual length of 0.
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(FIRSTNAME,0,2,CODEUNITS32) 'J' -- x'4AC3BC'
SUBSTRING(UTF8_VAR, 2, 5, OCTETS)