In this article I will explain with an example, how to Check if String contains Substring in SQL Server.
 
 

Checking if String contains Substring in SQL Server

First, a variable with Name is defined.
Then inside, the IF Condition CHARINDEX function is used for checking substring.
The CHARINDEX has following parameters:
1. expressionToFind – A character expression containing the sequence to find. expressionToFind has an 8000 character limit.
2. expressionToSearch - A character expression to search.
3. start_location (Optional) - An integer or bigint expression at which the search starts. If start_location is not specified, has a negative value, or has a zero (0) value, the search starts at the beginning of expressionToSearch.
If the value is greater than zero then Found is returned, otherwise Not Found is return.
Note: For more details on CHARINDEX, please refer here.
 
DECLARE @Name VARCHAR(50) = 'Mudassar Khan'
IF CHARINDEX('Mudassar', @Name) > 0
BEGIN
    SELECT 'Found'
END
ELSE
BEGIN
    SELECT 'Not Found'
END 
 
 

Downloads