• expression_to_find : In this parameter, we specify a character or string that we want to search in another string
  • expression_to_search: We can specify a string or sentence in which we want to search expression_to_find
  • start_location: It is an optional parameter. We can specify an integer value in this parameter to specify start location. If we want to search expression_to_find in expression_to_search with a specified start location, we can specify it. By default, if we do not mention any value in this parameter, it starts a search from the index position 0

    Example 1: Search a character position in a string

    In this example, we want to find position of @ character in a specified email address [email protected] In the following screenshot, we can see position of each character in the email address string. The character ‘ @’ is in position 17. We get this position in output of SQL CHARINDEX. Suppose we want to get the position of the second dot (.) in this email address. We can specify a value for an optional parameter to start searching from a specific position. It starts from a specific character position and checks for the character position. We still get the actual position of the character that is 23rd in this example.

    Example 3: Search a substring position in a specified string in SQL CHARINDEX

    In previous examples, we searched a specific character in a specified string. We can also search a substring as well in a string. In the following query, we declared a variable @ExpressionToSearch for the string and set a value on it. We want to search for substring Rajendra in this string.

    Example 4: Search a substring position in a specified string with multiple matching in SQL CHARINDEX

    Suppose we want to search a substring in a specified string. In the string we have multiple matching substrings. For example, in the following query, we want to search for SQLShack and find its position.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    DECLARE @ Name AS VARCHAR ( 100 ) = 'Explore SQL Server with articles on SQLShack' ;
    SELECT CASE
    WHEN CHARINDEX ( 'SQLShack' , @ Name ) > 0
    THEN 'Exists'
    ELSE 'Not Exists'
    END AS FindSubString ;
    SELECT CASE
    WHEN CHARINDEX ( 'Rajendra' , @ Name ) > 0
    THEN 'Exists'
    ELSE 'Not Exists'
    END AS FindSubString ;
    1
    2
    3
    4
    5
    6
    7
    8
    9
    DECLARE @ ExpressionToSearch varchar ( 100 )
    SET @ ExpressionToSearch = 'Explore SQL Server on SQLSHACK with Rajendra Gupta articles '
    SELECT CHARINDEX ( 'sqlshack' , @ ExpressionToSearch COLLATE Latin1_General_CS_AS ) AS 'CharacterPosition'
    SELECT CHARINDEX ( 'SQLShack' , @ ExpressionToSearch COLLATE Latin1_General_CS_AS ) AS 'CharacterPosition'
    SELECT CHARINDEX ( 'SQLSHACK' , @ ExpressionToSearch COLLATE Latin1_General_CS_AS ) AS 'CharacterPosition'
    If the target string varchar(max), nvarchar(max), it returns Big Int value else it returns Int data type By default, it performs a case insensitive search If there is no match found, it returns 0 in return

    Conclusion

    In this article, we explored SQL CHARINDEX function and its usage with various examples. Please feel free to provide feedback or ask questions in the comment section below.
  •