In SQL Server, you can use either the CHARINDEX()
function or the PATINDEX()
function to find a string within a string. These are Transact-SQL string functions, and they’re also available on Azure databases.
On the surface, these functions appear to do exactly the same thing, and in many cases, you could use whichever you prefer to use.
However, there a a couple of distinctions that could dictate which function you decide to use in certain scenarios. These can be summarized by the following:
PATINDEX()
allows you to use wildcard characters to search for patterns.CHARINDEX()
doesn’t.CHARINDEX()
accepts a third argument which allows you to specify the start position of the search.PATINDEX()
doesn’t.
More detail on these points below.