Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

PATINDEX

Example

Retrieve the position of a pattern within a string.

SELECT PATINDEX(‘%schools%’‘W3Schools.com’);

Definition and Usage

The PATINDEX() function returns the starting position of a pattern within a string, and returns 0 if the pattern is not found.

Note: The search conducted by PATINDEX() is case-insensitive, and the first position in the string is considered 1.

Syntax

PATINDEX(%pattern%, string)

Parameter Values

Parameter

Description

%pattern%

Required: The pattern to search for, enclosed in ‘%’ symbols. Other supported wildcards include:

  • ‘%’ – Matches any string of any length (including zero length).
  • ‘_’ – Matches a single character.
  • [] – Matches any character within the brackets, e.g., [xyz].
  • [^] – Matches any character not within the brackets, e.g., [^xyz].

string

Required: The string in which the pattern will be searched.

Technical Details

Works in:

 SQL Server (since 2008), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

More Examples

Example

Retrieve the position of a pattern within a string.

SELECT PATINDEX(‘%s%com%’‘W3Schools.com’);

Example

Retrieve the position of a pattern within a string.

SELECT PATINDEX(‘%[ol]%’‘W3Schools.com’);

Example

Retrieve the position of a pattern within a string.

SELECT PATINDEX(‘%[z]%’‘W3Schools.com’);