T-SQL Tutorial

Charindex TSQL Tutorial


Search an expression in an string expression and returns its starting position if found.
The charindex function can be used to return the starting position of an expression or to return a certain position in a string.
The charindex function can also perform a case-sensitive or case-insensitive search.

Charindex function syntax

charindex ( search_expression ,string_expression [ , start_location ] )

Charindex Example :

USE model;
GO
DECLARE @string varchar(64);

SELECT @string = 'My example';
SELECT CHARINDEX('y', @string) as 'Col_1',
CHARINDEX('x', @string) as 'Col_2',
CHARINDEX('le', @string,8) as 'Col_3';
GO

Result:

Col_1Col_2Col_3
259

See also: T-SQL Functions -> Concat -> Left -> Len -> Lower -> Ltrim