CHARINDEX function [String]

Function

Returns the position of the first occurrence of one string in another.

Syntax

CHARINDEX ( string-expression1, string-expression2 )

Parameters

string-expression1 The string you are searching for. This string is limited to 255 bytes.

string-expression2 The string to be searched.

Example

The statement:

SELECT emp_lname, emp_fname
FROM employee
WHERE CHARINDEX('K', emp_lname ) = 1

returns the following values:

emp_lname

emp_fname

Klobucher

James

Kuo

Felicia

Kelly

Moira

The position of the first character in the string being searched is 1.

Usage

If the string being searched contains more than one instance of the other string, then CHARINDEX returns the position of the first instance.

If the string being searched does not contain the other string, then CHARINDEX returns 0.

Standards and compatibility

See also

“SUBSTRING function [String]”