Pages

Monday, August 19, 2013

SQL Server Check if String Contains Specific Word / String with CHARINDEX Function

Here I will explain how to check if string contains specific word in SQL Server using CHARINDEX function or SQL Server check if string contains specific substring with CHARINDEX function. 

To check if string contains specific word in SQL Server we can use CHARINDEX function.

CHARINDEX Function

This function is used to search for specific word or substring in overall string and returns its starting position of match. In case if no word found then it will return 0 (zero).

Syntax of CHARINDEX

CHARINDEX ( StringToFind ,OverAllStringToSearch [ , start_location ] )

Example 1

DECLARE @str VARCHAR(250)
SET @str='Welcome to dotnetstores.blogspot.in'
SELECT CHARINDEX('dotnet',@str)

Output

Once we run above query we will get output like as shown below

-----------------------
12

Example 2

DECLARE @str VARCHAR(250)
SET @str='Welcome to dotnetstores.blogspot.in'
SELECT CHARINDEX('dotnet',@str,7)

Output

Once we run above query search will start from 7th position of string and we will get output like as shown below

-----------------------
12

No comments:

Post a Comment