PATINDEX Function Example in MS SQL Server


PATINDEX is similar to CHARINDEX in MSSQL — it checks whether a string exists in another string and returns its first occurring position. The difference is that PATINDEX supports wildcard characters ('%' and '_'), just like the LIKE operator.

MS SQL Server

Syntax:

PATINDEX ( '%pattern%' , expression )

Example:

select PATINDEX('%is%','This is demo')
---------------------
3

select PATINDEX('% is%','This is demo')
---------------------
5

Patindex example

The following example uses % and _ wildcards to find the position at which the pattern 'Sm', followed by any one character, and 'th' starts in the specified string:

select PATINDEX('%Sm_th%','This is Smith')
----------
9

select PATINDEX('%Sm_th%','This is Smyth')
----------
9

MSSQL PATINDEX function

PATINDEX function Example

MySQL

There is no similar function available in MySQL.

Back to Converting Functions from MSSQL to MySQL