What is SQL Server CHARINDEX() Function?
Overview
The CHARINDEX() function searches for a substring in a given string and its index is returned. If a substring is not found, 0 is returned.
Note: The search done by this function is not case-sensitive.
Syntax
CHARINDEX(substring, string, start)
Parameters
This method takes three parameters:
substring: The substring to be searched.string: The string from which the substring is to be searched.start: This is an optional parameter. It is only used when we have to specify a starting position in the string apart from the start of the string.
Example 1
In this example, the substring is ive and the string is Educative. The given sub-string is searched from the string and the starting index of the substring is returned. In this case, 7 is returned.
SELECT CHARINDEX('ive', 'Educative') ;
Example 2
In this example, the substring is ER and the string is America. ER is in capital words but as mentioned earlier the search will be case-insensitive so there is no difference in searching. In this case, 3 is returned as ER starts from the third position in the string.
SELECT CHARINDEX('ER', 'America')