CHARINDEX function [String]

Function

Returns the position of the first occurrence of a specified string in another string.

Syntax

CHARINDEXstring-expression1, string-expression2 )

Parameters

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

string-expression2 The string to be searched. The position of the first character in the string being searched is 1.

Example

The statement:

SELECT Surname, GivenName
FROM Employees
WHERE CHARINDEX('K', Surname ) = 1

returns the following values:

Surname

GivenName

Klobucher

James

Kuo

Felicia

Kelly

Moira

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 string being searched contains more than one instance of the specified string, CHARINDEX returns the position of the first instance.

If the string being searched does not contain the specified string, CHARINDEX returns zero (0).

Searching for a zero-length string returns 1.

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

CHARINDEX returns a 32 bit signed integer position for CHAR and VARCHAR columns.

Standards and compatibility

See also

“SUBSTRING function [String]”

Chapter 9, “Function Support” in Unstructured Data Analytics in Sybase IQ