SUBSTRING, CHARINDEX and PATINDEX in SQL Server


SUBSTRING is used to find the portion of a string from a given string.

Syntax: SUBSTRING ( expression ,start , length )

SELECT SUBSTRING('SQL FIGHTER',5,7) AS SUBSTRING_VALUE


substring in sql server


CHARINDEX is used to find the starting position of a specified expression in a given string.

Syntax: CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )

SELECT CHARINDEX('L','HELLO WORLD') AS CHARINDEX_VALUE_1

SELECT CHARINDEX('L','HELLO WORLD',7) AS CHARINDEX_VALUE_2


charindex in sql server


PATINDEX is used to find the starting position of the first occurrence of a pattern in a given string.

Syntax: PATINDEX ( '%pattern%' ,expression )

SELECT PATINDEX('%SERV%','SQL SERVER SERVICE') AS PATINDEX_VALUE


patindex in sql server