The CHARINDEX string function returns the starting position of the specified expression in a character string. It accepts three parameters with the third parameter being optional.

CHARINDEX ( expression1, expression2, [ , start_location ] )

The first parameter is the expression that contains the sequence of characters to be found. The second parameter is the expression searched for the specified sequence. This is typically a column from a table. The third parameter, which is optional, is the character position to start searching expression1 in expression2.

Another use of the CHARINDEX string function, which is not too obvious, is in sorting a result set. Let’s assume you have a table which contains a U.S. State Code and instead of sorting the result alphabetically based on the U.S. State Code you want it sorted by certain states, like CA, FL, TX, NY in that order. This can be accomplished using the CHARINDEX string function: