LOCATE_IN_STRING scalar function

The LOCATE_IN_STRING function returns the starting position of a string (called the search-string ) within another string (called the source-string).

Read syntax diagramSkip visual syntax diagramLOCATE_IN_STRING(source-string ,search-string ,start,instance ,CODEUNITS16CODEUNITS32OCTETS )

The schema is SYSIBM

If the search-string is not found and neither argument is null, the result is zero. If the search-string is found, the result is a number from 1 to the actual length of the source-string. The search is done using the collation of the database, unless search-string or source-string is defined as a binary string or as FOR BIT DATA, in which case the search is done using a binary comparison.

If the optional start is specified, it indicates the character position in the source-string at which the search is to begin. If the start is specified, an instance number can also be specified. The instance argument is used to determine the position of a specific occurrence of search-string within source-string. An optional string unit can be specified to indicate in what units the start and result of the function are expressed.

If the search-string has a length of zero, the result returned by the function is 1. If the source-string has a length of zero, the result returned by the function is 0. If neither condition exists, and if the value of search-string is equal to an identical length of a substring of contiguous positions within the value of source-string, the result returned by the function is the starting position of that substring within the source-string value; otherwise, the result returned by the function is 0.

source-string
An expression that specifies the string in which the search is to take place. The expression must return a value that is a built-in string, numeric, Boolean, or datetime data type. If the value is not a string data type, it is implicitly cast to VARCHAR before evaluating the function.
search-string
An expression that specifies the string that is the object of the search. The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, binary string, numeric, Boolean, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, or binary string data type, it is implicitly cast to VARCHAR before evaluating the function. The expression cannot be specified by a LOB file reference variable.
start
An expression that specifies the position within source-string at which the search for a match is to start. 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 the integer is greater than zero, the search begins at start and continues for each position to the end of the string. If the value of the integer is less than zero, the search begins at LENGTH(source-string) + start + 1 and continues for each position to the beginning of the string.

If start is not specified, the default is 1. If OCTETS is specified and source-string is graphic data, the value of the integer must be odd (SQLSTATE 428GC). If the value of the integer is zero, an error is returned (SQLSTATE 42815).

instance
An expression that specifies which instance of search-string to search for within source-string. 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 instance is not specified, the default is 1. The value of the integer must be greater than or equal to 1 (SQLSTATE 42815).
CODEUNITS16, CODEUNITS32, or OCTETS
Specifies the string unit of start and the result. CODEUNITS16 specifies that start and the result are to be expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that start and the result are to be expressed in 32-bit UTF-32 code units. OCTETS specifies that start and the result are to be expressed in bytes.

If a string unit is specified as CODEUNITS16 or CODEUNITS32, and search-string or source-string is a binary string or FOR BIT DATA, an error is returned (SQLSTATE 428GC). 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 a string unit is not explicitly specified and if source-string is a character or graphic string, the string units of source-string determines the unit that is used for the result and for start (if specified). Otherwise, they are expressed in bytes.

If a locale-sensitive UCA-based collation is used for this function, then the CODEUNITS16 option offers the best performance characteristics.

For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see String units in built-in functions in Character strings.

The first and second arguments must have compatible string types. For more information about compatibility, see Rules for string conversions. In a Unicode database, if one string argument is character (not FOR BIT DATA) and the other string argument is graphic, then the search-string is converted to the data type of the source-string for processing. If one argument is character FOR BIT DATA, the other argument must not be graphic (SQLSTATE 42846).

At each search position, a match is found when the substring at that position and LENGTH(search-string) - 1 values to the right of the search position in source-string, is equal to search-string.

The result of the function is a large integer. The result is the starting position of the instance of search-string within source-string. The value is relative to the beginning of the string (regardless of the specification of start). If any argument can be null, the result can be null; if any argument is null, the result is the null value.

INSTR can be used as a synonym for LOCATE_IN_STRING.

The INSTRB scalar function is equivalent to invoking the LOCATE_IN_STRING function with OCTETS (where allowed) specified to indicate that start position and the result are expressed in bytes.

Examples

  • Example 1: Locate the character ß in the string Jürgen lives on Hegelstraße by searching from the end of the string, and set the host variable POSITION with the position, as measured in CODEUNITS32 units, within the string.
        SET :POSITION = LOCATE_IN_STRING('Jürgen lives on Hegelstraße',
                                        'ß',-1,CODEUNITS32);
    
    The value of host variable POSITION is set to 26.
  • Example 2: Find the location of the third occurrence of the character N in the string WINNING by searching from the start of the string and then set the host variable POSITION with the position of the character, as measured in bytes, within the string.
        SET :POSITION =
        LOCATE_IN_STRING('WINNING','N',1,3,OCTETS);
    
    The value of host variable POSITION is set to 6.