// strings reference

SQL PATINDEX function

SQL Server search that returns the first position where a LIKE-style wildcard pattern matches inside a string.

Category
Strings
Returns
integer
In this category
89 of 172

Syntax

PATINDEX('%pattern%', string)

Engine support

EngineSupportNotes
SQL Server Yes Supported under this name.
PostgreSQL Different name or syntax regexp_instr (PostgreSQL 15+) takes a regex instead of a LIKE pattern.
MySQL Different name or syntax REGEXP_INSTR in MySQL 8.0.4+ takes a regex.
SQLite Not built in No PATINDEX; INSTR finds literal substrings only.
Oracle Different name or syntax REGEXP_INSTR takes a regex.

Examples

SQL Server

SELECT PATINDEX('%[0-9]%', address) AS first_digit
FROM addresses;

SQL Server

SELECT PATINDEX('%[^a-z ]%', comment) AS first_bad_char
FROM comments;

Good to know

  • The pattern usually needs % on both sides. PATINDEX('abc', s) only matches when the whole string is 'abc'; '%abc%' finds it anywhere.
  • Patterns use LIKE syntax, not regex: % and _ wildcards, [abc] and [a-z] character classes, [^abc] negation. No +, * or alternation.
  • Returns 0 for no match and NULL if either argument is NULL. There is no ESCAPE clause as in LIKE; match a literal % or _ by bracketing it: '%[%]%'.

Questions about PATINDEX

What is the difference between PATINDEX and CHARINDEX?

CHARINDEX searches for a literal substring and accepts a start position; PATINDEX searches for a LIKE pattern and always starts at 1. Use PATINDEX when you need the first character that matches a class such as [0-9].

Does PATINDEX support full regular expressions?

No, only the LIKE wildcard set: %, _ and bracketed character classes. SQL Server 2025 and Azure SQL Database add REGEXP_INSTR for real regex; on older versions the options are CLR functions or the application.

Why does PATINDEX return 0 when the substring is clearly there?

Without % wildcards the pattern must match the whole string. Write PATINDEX('%abc%', col), not PATINDEX('abc', col).

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.