DB2 Scalar functions - POSITION
The POSITION function returns the starting position of the first occurrence of one string within another string.
The schema is SYSIBM.
The string the POSITION function searches for is called the search-string. The string it searches in is called the source-string. The POSITION function returns the starting position of the first occurrence of one string (called the search-string) within another string (called the source-string). If 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 source-string, expressed in the string unit that is explicitly specified. 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 source-string has an actual length of 0, the result of the function is 0. If search-string has an actual length of 0 and source-string is not null, the result of the function is 1.
These rules are similar to those that are described for pattern-expression for the LIKE predicate.
If a string unit is specified as CODEUNITS16 or CODEUNITS32, and search-string or source-string is a binary string or a FOR BIT DATA string, an error is returned (SQLSTATE 428GC).
If a string unit argument is not specified and both search-string and source-string are either a character string that is not FOR BIT DATA or is a graphic string, the default is CODEUNITS32. Otherwise, the default is OCTETS.
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.
String units in built-in functions
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).
Rules for string conversions
The result of the function is a large integer. If any argument can be null, the result can be null; if any argument is null, the result is the null value.
SELECT RECEIVED , SUBJECT , POSITION('GOOD BEER', NOTE_TEXT, OCTETS) FROM IN_TRAY WHERE POSITION('GOOD BEER', NOTE_TEXT, OCTETS) <> 0
SET :LOCATION = POSITION(ß', 'Jürgen lives on Hegelstraße', CODEUNITS32)
SET :LOCATION = POSITION('ß', 'Jürgen lives on Hegelstraße', OCTETS)
Assume that the variable UTF8_VAR contains the UTF-8 representation of the string.
SELECT POSITION('N', UTF8_VAR, CODEUNITS16), POSITION('N', UTF8_VAR, CODEUNITS32), POSITION('N', UTF8_VAR, OCTETS) FROM SYSIBM.SYSDUMMY1
Assume that the variable UTF16_VAR contains the UTF-16BE representation of the string.
SELECT POSITION('B', UTF16_VAR, CODEUNITS16), POSITION('B', UTF16_VAR, CODEUNITS32), POSITION('B', UTF16_VAR, OCTETS) FROM SYSIBM.SYSDUMMY1
SET :LOCATION = POSITION('Brown', 'The quick brown fox', CODEUNITS16)