DB2 Version 9.7 for Linux, UNIX, and Windows

POSITION scalar function

Read syntax diagramSkip visual syntax diagram
>>-POSITION----------------------------------------------------->

>--(--+-search-string--IN--source-string--USING--+-CODEUNITS16-+-+--)-><
      |                                          +-CODEUNITS32-+ |      
      |                                          '-OCTETS------' |      
      '-search-string--,--source-string--,--+-CODEUNITS16-+------'      
                                            +-CODEUNITS32-+             
                                            '-OCTETS------'             

The schema is SYSIBM.

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 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.

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, BLOB, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, VARGRAPHIC or BLOB data type, it is implicitly cast to VARCHAR before evaluating the function. The expression cannot be a BLOB file reference variable. The expression can be specified by any of the following:
  • A constant
  • A special register
  • A host variable
  • A scalar function whose operands are any of the above
  • An expression that concatenates (using CONCAT or ||) any of the above
  • An SQL procedure parameter

These rules are similar to those that are described for pattern-expression for the LIKE predicate.

source-string
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. The expression can be specified by any of the following:
  • A constant
  • A special register
  • A host variable (including a locator variable or a file reference variable)
  • A scalar function
  • A large object locator
  • A column name
  • An expression that concatenates (using CONCAT or ||) any of the above
CODEUNITS16, CODEUNITS32, or OCTETS
Specifies the string unit of the result. CODEUNITS16 specifies that the result is to be expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that the result is to be expressed in 32-bit UTF-32 code units. OCTETS specifies that the result is 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 bit data, an error is returned (SQLSTATE 428GC). If a string unit is specified as OCTETS and search-string and source-string are binary strings, an error is returned (SQLSTATE 42815).

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 on 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).

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.

Examples: