In SQL Server, you can use the T-SQL CHARINDEX()
function or the PATINDEX()
function to find a string within another string. Here’s a quick overview of each function.
The CHARINDEX() Function
This function accepts 3 arguments; the string to find, the string to search, and an optional start position.
The CHARINDEX()
syntax goes like this:
CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )
Where expressionToFind is the expression you want to find in the other string, and expressionToSearch is the other string. The optional start_location can be used to specify a position within expressionToSearch for which to start searching.
Note that only the position of the first occurrence is returned.