SQL Server – CHARINDEX Function with example

SQL Server CHARINDEX function returns starting position of a pattern you specify.
CHARINDEX takes two parameter, first parameter is specify pattern to be search in expression.

Syntax

SELECT CHARINDEX(‘pattern’,’Expression or string’)
1. Pattern -> Value to be search in string or expression
2. Expression or string -> Specified pattern to be under second parameter value.

Example of CHARINDEX

SELECT CHARINDEX(‘Hello’,’Hello World’) as ‘Result of CHARINDEX’
Here, We are looking for “Hello” word in given string “Hello World”. CHARINDEX Returns position of “Hello” as output.

Output

Result of CHARINDEX
——————–
1
Now we will try search specify character in given string using CHARINDEX function
SELECT CHARINDEX(‘o’,’Hello World’) as ‘Result of CHARINDEX’
Here,it will return 5 as starting position.

Output

Result of CHARINDEX
—————-
5

Leave a Reply

  

  

  


6 − = 5