CHARINDEX Function [String]

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

Syntax

CHARINDEXstring-expression1, string-expression2 )

Parameters

Parameter

Description

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.

Returns

INT

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.

If you are licensed to use the Unstructured Data Analytics functionality, you can use this function with large object data. See Unstructured Data Analytics in Sybase IQ > Function Support.

Standards and Compatibility

  • SQL—Vendor extension to ISO/ANSI SQL grammar.

  • Sybase—Compatible with Adaptive Server Enterprise.

Related reference
SUBSTRING Function [String]