CHARINDEX: returns the starting point of the first occurrence of one string of characters within another string : CHARINDEX « String Functions « SQL Server / T-SQL






CHARINDEX: returns the starting point of the first occurrence of one string of characters within another string


1> -- CHARINDEX: returns the starting point of the first occurrence of one string of characters within
2> -- another string.
3>
4> -- A value of 0 is returned if the string is not found.
5>
6> -- CHARINDEX does not take into account whether the strings are upper or lower case.
7> -- In the example below you would hope that the CHARINDEX function would find the last Wrox, but it actually finds the WROX before that.
8>
9> DECLARE @STRING_TO_SEARCH_FOR varchar(4)
10> DECLARE @STRING_TO_SEARCH_WITHIN varchar(100)
11>
12> SET @STRING_TO_SEARCH_FOR = "a"
13> SET @STRING_TO_SEARCH_WITHIN = "abcdeAabcde"
14>
15> SELECT CHARINDEX(@STRING_TO_SEARCH_FOR, @STRING_TO_SEARCH_WITHIN, 16)
16>
17> GO

-----------
          0

(1 rows affected)
1>
           
       








Related examples in the same category

1.CHARINDEX(): find the first occurrence of a substring within another string
2.CHARINDEX('Mars', 'The stars near Mars are far from ours')
3.CHARINDEX: Get index of the delimiting space
4.Combine CHARINDEX with substring