CHARINDEX Function [String]

The CHARINDEX function returns a 64-bit signed integer containing the position of the first occurrence of the specified string in the large object column or variable parameter. For CHAR and VARCHAR columns, CHARINDEX returns a 32-bit signed integer position.

Syntax

CHARINDEXstring-expression, large -object-column )

Parameters

string-expression – the string of up to 255 bytes, for which you are searching.

large-object-column – the name of the LONG VARCHAR or LONG BINARY column or variable.

Usage

  • All the positions or offsets, returned or specified, in the CHARINDEX function are always character offsets and may be different from the byte offset for multibyte data.

  • If the large object cell being searched contains more than one instance of string-expression, CHARINDEX returns only the position of the first instance.

  • If the column does not contain the string, the CHARINDEX function returns zero (0).

  • Searching for a string longer than 255 bytes returns NULL.

  • Searching for a zero-length string returns 1.

  • If any of the arguments is NULL, the result is NULL.

  • CHARINDEX supports searching LONG VARCHAR and LONG BINARY columns and LONG VARCHAR and LONG BINARY variables of any size of data. Currently, a SQL variable can hold up to 2GB - 1 in length.