DB2 Version 9.7 for Linux, UNIX, and Windows

SUBSTRB scalar function

Read syntax diagramSkip visual syntax diagram
>>-SUBSTRB--(--string--,--start--+-----------+--)--------------><
                                 '-,--length-'      

The schema is SYSIBM.

The SUBSTRB function returns a substring of a string, beginning at a specified position in the string. Lengths are calculated in bytes.

The SUBSTRB function is available starting with version 9.7 Fix Pack 1.

string
An expression that specifies the string from which the result is derived.

The expression must return a value that is a built-in string, numeric, or datetime data type. If the value is not a string data type it is implicitly cast to VARCHAR before evaluating the function. A substring of string is zero or more contiguous bytes of string. In a Unicode database, if the value is a graphic data type, it is implicitly cast to a character string data type before evaluating the function.

start
An expression that specifies the start position in string of the beginning of the result substring. The expression must return a value that is a built-in numeric, CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type. If the value is not of type INTEGER, it is implicitly cast to INTEGER before evaluating the function.

If start is positive, then the start position is calculated from the beginning of the string. If start is greater than the length of string, then a zero length string is returned.

If start is negative, then the start position is calculated from the end of the string and by counting backwards. If the absolute value of start is greater than the length of string, then a zero length string is returned.

If start is 0, then a start position of 1 is used.

length
An expression that specifies the length of the result in bytes. If specified, the expression must return a value that is a built-in numeric, CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type. If the value is not of type INTEGER, it is implicitly cast to INTEGER before evaluating the function.

If the value of length is greater than the number of bytes from the start position to the end of the string, the result length is the length of the first argument minus the start position plus one.

If the value of length is less than or equal to zero, the result of SUBSTRB is a zero length string.

The default for length is the number of bytes from the position specified by start to the last byte of string.

If string is a CHAR or VARCHAR data type, the result of the function is a VARCHAR data type. If it is a CLOB, the result of the function is a CLOB. If it is a BLOB, the result of the function is a BLOB. If the first argument is a host variable that is not a binary string and not a FOR BIT DATA character string, the code page of the result is the section code page; otherwise, it is the code page of the first argument.

The length attribute of the result is the same as the length attribute of the first argument unless both start and length arguments are specified and defined as constants. In this case, the length attribute of the result is determined as follows:
  • If length is a constant which is less than or equal to zero, the length attribute of the result is zero.
  • If start is not a constant, but length is a constant, the length attribute of the result is the minimum of the length attribute of the first argument and length.
  • If start is a constant, but length is not a constant or not specified, the length attribute of the result is the length attribute of the first argument minus the start position, plus one.
  • If start and length are constants, the length attribute of the result is the minimum of the following values:
    • length
    • The length attribute of the first argument minus the start position plus one

If any argument of the SUBSTRB function can be null, the result can be null; if any argument is null, the result is the null value.

Notes

Examples

  • Assume the host variable NAME (VARCHAR(50)) has a value of 'BLUE JAY' and the host variable SURNAME_POS (INTEGER) has a value of 6.
       SUBSTRB(:NAME, :SURNAME_POS)
    Returns the value 'JAY'.
       SUBSTRB(:NAME, :SURNAME_POS,1)
    Returns the value 'J'.
  • Select all rows from the PROJECT table which end in 'ING'.
       SELECT * FROM PROJECT
          WHERE SUBSTRB(PROJNAME,-3) = 'ING'