CHARINDEX
CHARINDEX is a function used in SQL to find the position of a substring in a string. It returns the starting position of the first instance of a substring within a string.
CHARINDEX( expressionToFind ,expressionToSearch [ , start_location ] )
- expressiontofind: This is the string expression that will be searched for within expressionToSearch.
- expressiontosearch: This parameter is the string expression in which to search the occurrence of expressionToFind.
- start_location: Optional. This specifies where the search should start within the expressionToSearch. If not specified, the search starts from the beginning.
Example
SELECT CHARINDEX('SQL', 'Learn SQL') AS Position;
Output
Position-----------7
Explanation
The CHARINDEX function returns the position of the first occurrence of the substring ‘SQL’ in the string ‘Learn SQL’. The position is returned as 7, which is the starting position of ‘SQL’ in ‘Learn SQL’.