Check if String contains Substring in SQL Server

 Introduction: Hello Guys, “Welcome back” Today, i am here with another one new great article. In this article I explained the implementation about how to Check if String contains Substring in SQL Server. This article is applicable for all versions of SQLServer i.e. 2005, 2008, 2012, 2014, 2019, 2022 etc.

Join our Channel  on Whtsapp and Telagram for All Updates                                          


Checking if String contains Substring in SQL Server

Here we need to define variable with Name first. After that CHARINDEX function will be for checking substring used with the IF Condition

To the CHARINDEX function need to pass the following parameters:

1. expressionToFindAn expression can be a phrase, an algorithm, or a search query, while "to find" is a verb meaning to locate or discover something. A "find expression" (or find string/query) is a specific type of query that uses particular syntax to locate data within a system, much like a WHERE clause in SQL or a regular expression A character expression containing the sequence to find.expressionToFind has an 8000 character limit.

2. expressionToSearch - An expression to search refers to using a regular expression (regex), which is a sequence of characters that defines a search pattern to find specific text within a larger string or document.  Regular expressions use special characters called metaccharacters . for any character, * for zero or more occurrences, or \d for a digit) to create flexible and powerful search patterns for tasks such as finding email addresses, URLs, or specific data formats. You use these patterns with a function or method in a programming language or a text editor to execute the search.

3. start_location (Optional) - This refers to the process of initiating the SQL Server Database Engine or other related services (like SQLServer Agent, SQL Server Browser). Navigate to "SQL Server Services," right-click the desired service (e.g., "SQL Server (MSSQLSERVER)" for the default instance), and select "Start." 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. Below I am going to explain the sql query for Check if String contains Substring in SQL Server. If the value will be greater than zero then Found is returned, otherwise Not Found is return. For more detailed study  on CHARINDEX, please follow the Article.

DECLARE @username VARCHAR(50) = 'SanjeevKumar'

IF CHARINDEX('Sanjeev', @username) >0

BEGIN

    SELECT 'Found'

END

ELSE

BEGIN

    SELECT 'Not Found'

END 

Conclusion: In above code, I have been explained implementation about how to how to Check if String contains Substring in SQL Server. This code is very helpful for every developer. Bye bye and take care of yourself Developers. We will come back shortly with the new article.

Regards

Using Asp.net

No comments:

Post a Comment