charindex

Returns an integer representing the starting position of an expression.

Syntax

charindex(expression1, expression2 [, start])

Parameters

Examples

Usage

  • charindex, a string function, searches expression2 for the first occurrence of expression1 and returns an integer representing its starting position. If expression1 is not found, charindex returns 0.

  • If expression1 contains wildcard characters, charindex treats them as literals.

  • If expression2 is NULL, returns 0.

  • If a varchar expression is given as one parameter and a unichar expression as the other, the varchar expression is implicitly converted to unichar (with possible truncation).

  • If only one of expression1 or expression2 is a locator, the datatype of the other expression must be implicitly convertible to the datatype of the LOB referenced by the locator.

  • When expression1 is a locator, the maximum length of the LOB referenced by the locator is 16KB.

  • The start value is interpreted as the number of characters to skip before starting the search for varchar, univarchar, text_locator, and unitext_locator datatypes, and as the number of bytes for binary and image_locator datatypes.

  • The maximum length of expression1 is 16,384 bytes.

  • If a varchar expression is given as one parameter and a unichar expression as the other, the varchar expression is implicitly converted to unichar (with possible truncation).

See also Transact-SQL Users Guide.

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

Any user can execute charindex.

Related reference
patindex