Skip to content

Commit 3e400b3

Browse files
authored
Merge pull request #34745 from MikeRayMSFT/250718-patindex-uuf
UUF: Update patindex-transact-sql.md
2 parents 0681d66 + 3e99674 commit 3e400b3

File tree

1 file changed

+141
-124
lines changed

1 file changed

+141
-124
lines changed
Lines changed: 141 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "PATINDEX (Transact-SQL)"
3-
description: "PATINDEX (Transact-SQL)"
3+
description: PATINDEX returns the starting position of the first occurrence of a pattern in a specified expression, or zero.
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.date: "07/19/2016"
6+
ms.reviewer: randolphwest
7+
ms.date: 07/18/2025
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -18,162 +19,178 @@ helpviewer_keywords:
1819
- "PATINDEX function"
1920
dev_langs:
2021
- "TSQL"
21-
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric"
22+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric"
2223
---
2324
# PATINDEX (Transact-SQL)
25+
2426
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw.md)]
2527

26-
Returns the starting position of the first occurrence of a pattern in a specified expression, or zero if the pattern is not found, on all valid text and character data types.
27-
28-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
29-
30-
## Syntax
31-
32-
```
33-
PATINDEX ( '%pattern%' , expression )
34-
```
35-
28+
Returns the starting position of the first occurrence of a pattern in a specified expression, or zero if the pattern isn't found, on all valid text and character data types.
29+
30+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
31+
32+
## Syntax
33+
34+
```syntaxsql
35+
PATINDEX ( '%pattern%' , expression )
36+
```
37+
3638
## Arguments
37-
*pattern*
38-
Is a character expression that contains the sequence to be found. Wildcard characters can be used; however, the % character must come before and follow *pattern* (except when you search for first or last characters). *pattern* is an expression of the character string data type category. *pattern* is limited to 8000 characters.
39-
40-
> [!NOTE]
41-
> While traditional regular expressions are not natively supported in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], similar complex pattern matching can be achieved by using various wildcard expressions. See the [String Operators](../../t-sql/language-elements/string-operators-transact-sql.md) documentation for more detail on wildcard syntax.
42-
43-
*expression*
44-
Is an [expression](../../t-sql/language-elements/expressions-transact-sql.md), typically a column that is searched for the specified pattern. *expression* is of the character string data type category.
45-
46-
## Return Types
47-
**bigint** if *expression* is of the **varchar(max)** or **nvarchar(max)** data types; otherwise **int**.
48-
49-
## Remarks
50-
If either *pattern* or *expression* is NULL, PATINDEX returns NULL.
51-
52-
The starting position for PATINDEX is 1.
53-
54-
PATINDEX performs comparisons based on the collation of the input. To perform a comparison in a specified collation, you can use COLLATE to apply an explicit collation to the input.
55-
56-
## Supplementary Characters (Surrogate Pairs)
57-
When using SC collations, the return value will count any UTF-16 surrogate pairs in the *expression* parameter as a single character. For more information, see [Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md).
58-
59-
0x0000 (**char(0)**) is an undefined character in Windows collations and cannot be included in PATINDEX.
60-
61-
## Examples
62-
63-
### A. Simple PATINDEX example
64-
The following example checks a short character string (`interesting data`) for the starting location of the characters `ter`.
65-
66-
```sql
67-
SELECT position = PATINDEX('%ter%', 'interesting data');
68-
```
69-
70-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
7139

40+
#### *pattern*
41+
42+
A character expression that contains the sequence to be found. Wildcard characters can be used; however, the % character must come before and follow *pattern* (except when you search for first or last characters). *pattern* is an expression of the character string data type category. *pattern* is limited to 8,000 characters.
43+
44+
> [!NOTE]
45+
> While traditional regular expressions aren't natively supported in [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and earlier versions, similar complex pattern matching can be achieved by using various wildcard expressions. See the [String operators](../language-elements/string-operators-transact-sql.md) documentation for more detail on wildcard syntax. For information about regular expression functions in [!INCLUDE [sssql25-md](../../includes/sssql25-md.md)], see [Regular expressions functions](regular-expressions-functions-transact-sql.md).
46+
47+
#### *expression*
48+
49+
An [expression](../language-elements/expressions-transact-sql.md), typically a column that is searched for the specified pattern. *expression* is of the character string data type category.
50+
51+
## Return types
52+
53+
**bigint** if *expression* is of the **varchar(max)** or **nvarchar(max)** data types; otherwise **int**.
54+
55+
## Remarks
56+
57+
If *pattern* is `NULL`, `PATINDEX` returns `NULL`.
58+
59+
If expression is `NULL`, `PATINDEX` returns an error.
60+
61+
The starting position for `PATINDEX` is `1`.
62+
63+
`PATINDEX` performs comparisons based on the collation of the input. To perform a comparison in a specified collation, you can use `COLLATE` to apply an explicit collation to the input.
64+
65+
## Supplementary characters (Surrogate pairs)
66+
67+
When you use collations with supplementary characters (SC), the return value counts any UTF-16 surrogate pairs in the *expression* parameter as a single character. For more information, see [Collation and Unicode support](../../relational-databases/collations/collation-and-unicode-support.md).
68+
69+
`0x0000` (**char(0)**) is an undefined character in Windows collations and can't be included in `PATINDEX`.
70+
71+
## Examples
72+
73+
### A. Basic PATINDEX example
74+
75+
The following example checks a short character string (`interesting data`) for the starting location of the characters `ter`.
76+
77+
```sql
78+
SELECT PATINDEX('%ter%', 'interesting data') AS position;
7279
```
80+
81+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
82+
83+
```output
7384
position
7485
--------
7586
3
7687
```
77-
78-
### B. Using a pattern with PATINDEX
79-
The following example finds the position at which the pattern `ensure` starts in a specific row of the `DocumentSummary` column in the `Document` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
80-
81-
```sql
82-
SELECT position = PATINDEX('%ensure%',DocumentSummary)
83-
FROM Production.Document
84-
WHERE DocumentNode = 0x7B40;
85-
GO
86-
```
87-
88-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
89-
88+
89+
### B. Use a pattern with PATINDEX
90+
91+
The following example finds the position at which the pattern `ensure` starts in a specific row of the `DocumentSummary` column in the `Document` table in the [!INCLUDE [ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
92+
93+
```sql
94+
SELECT PATINDEX('%ensure%', DocumentSummary) AS position
95+
FROM Production.Document
96+
WHERE DocumentNode = 0x7B40;
97+
GO
9098
```
99+
100+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
101+
102+
```output
91103
position
92-
--------
93-
64
94-
```
95-
96-
If you do not restrict the rows to be searched by using a `WHERE` clause, the query returns all rows in the table and reports nonzero values for those rows in which the pattern was found, and zero for all rows in which the pattern was not found.
97-
98-
### C. Using wildcard characters with PATINDEX
99-
The following example uses % and _ wildcards to find the position at which the pattern `'en'`, followed by any one character and `'ure'` starts in the specified string (index starts at 1):
100-
101-
```sql
102-
SELECT position = PATINDEX('%en_ure%', 'Please ensure the door is locked!');
103-
```
104-
105-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
106-
104+
--------
105+
64
107106
```
108-
position
109-
--------
110-
8
111-
```
112-
113-
`PATINDEX` works just like `LIKE`, so you can use any of the wildcards. You do not have to enclose the pattern between percents. `PATINDEX('a%', 'abc')` returns 1 and `PATINDEX('%a', 'cba')` returns 3.
114-
115-
Unlike `LIKE`, `PATINDEX` returns a position, similar to what `CHARINDEX` does.
116107

117-
### D. Using complex wildcard expressions with PATINDEX
118-
The following example uses the `[^]` [string operator](../../t-sql/language-elements/wildcard-character-s-not-to-match-transact-sql.md) to find the position of a character that is not a number, letter, or space.
108+
If you don't restrict the rows to be searched by using a `WHERE` clause, the query returns all rows in the table and reports nonzero values for those rows in which the pattern was found, and zero for all rows in which the pattern wasn't found.
109+
110+
### C. Use wildcard characters with PATINDEX
111+
112+
The following example uses % and _ wildcards to find the position at which the pattern `'en'`, followed by any one character and `'ure'` starts in the specified string (index starts at 1):
119113

120114
```sql
121-
SELECT position = PATINDEX('%[^ 0-9A-Za-z]%', 'Please ensure the door is locked!');
115+
SELECT PATINDEX('%en_ure%', 'Please ensure the door is locked!') AS position;
116+
```
117+
118+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
119+
120+
```output
121+
position
122+
--------
123+
8
122124
```
123-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
124125

126+
`PATINDEX` works just like `LIKE`, so you can use any of the wildcards. You don't have to enclose the pattern between percents. `PATINDEX('a%', 'abc')` returns 1 and `PATINDEX('%a', 'cba')` returns 3.
127+
128+
Unlike `LIKE`, `PATINDEX` returns a position, similar to what `CHARINDEX` does.
129+
130+
### D. Use complex wildcard expressions with PATINDEX
131+
132+
The following example uses the `[^]` [string operator](../language-elements/wildcard-character-s-not-to-match-transact-sql.md) to find the position of a character that isn't a number, letter, or space.
133+
134+
```sql
135+
SELECT PATINDEX('%[^ 0-9A-Za-z]%', 'Please ensure the door is locked!') AS position;
125136
```
137+
138+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
139+
140+
```output
126141
position
127142
--------
128143
33
129144
```
130145

131-
### E. Using COLLATE with PATINDEX
132-
The following example uses the `COLLATE` function to explicitly specify the collation of the expression that is searched.
133-
134-
```sql
135-
USE tempdb;
136-
GO
137-
SELECT PATINDEX ( '%ein%', 'Das ist ein Test' COLLATE Latin1_General_BIN) ;
138-
GO
139-
```
140-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
146+
### E. Use COLLATE with PATINDEX
147+
148+
The following example uses the `COLLATE` function to explicitly specify the collation of the expression that is searched.
149+
150+
```sql
151+
USE tempdb;
152+
GO
141153

154+
SELECT PATINDEX('%ein%', 'Das ist ein Test' COLLATE Latin1_General_BIN);
155+
GO
142156
```
157+
158+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
159+
160+
```output
143161
position
144162
--------
145163
9
146164
```
147165

148-
### F. Using a variable to specify the pattern
149-
The following example uses a variable to pass a value to the *pattern* parameter. This example uses the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
150-
151-
```sql
152-
DECLARE @MyValue VARCHAR(10) = 'safety';
153-
SELECT position = PATINDEX('%' + @MyValue + '%', DocumentSummary)
154-
FROM Production.Document
155-
WHERE DocumentNode = 0x7B40;
156-
```
157-
158-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
159-
166+
### F. Use a variable to specify the pattern
167+
168+
The following example uses a variable to pass a value to the *pattern* parameter. This example uses the [!INCLUDE [ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
169+
170+
```sql
171+
DECLARE @MyValue AS VARCHAR (10) = 'safety';
172+
173+
SELECT PATINDEX('%' + @MyValue + '%', DocumentSummary) AS position
174+
FROM Production.Document
175+
WHERE DocumentNode = 0x7B40;
160176
```
177+
178+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
179+
180+
```output
161181
position
162-
--------
182+
--------
163183
22
164-
```
165-
166-
## See Also
167-
[LIKE (Transact-SQL)](../../t-sql/language-elements/like-transact-sql.md)
168-
[CHARINDEX (Transact-SQL)](../../t-sql/functions/charindex-transact-sql.md)
169-
[LEN (Transact-SQL)](../../t-sql/functions/len-transact-sql.md)
170-
[Data Types (Transact-SQL)](../../t-sql/data-types/data-types-transact-sql.md)
171-
[String Functions (Transact-SQL)](../../t-sql/functions/string-functions-transact-sql.md)
172-
[(Wildcard - Character(s) to Match) (Transact-SQL)](../../t-sql/language-elements/wildcard-character-s-to-match-transact-sql.md)
173-
[(Wildcard - Character(s) Not to Match) (Transact-SQL)](../../t-sql/language-elements/wildcard-character-s-not-to-match-transact-sql.md)
174-
[_ (Wildcard - Match One Character) (Transact-SQL)](../../t-sql/language-elements/wildcard-match-one-character-transact-sql.md)
175-
[Percent character (Wildcard - Character(s) to Match) (Transact-SQL)](../../t-sql/language-elements/percent-character-wildcard-character-s-to-match-transact-sql.md)
176-
177-
184+
```
178185

186+
## Related content
179187

188+
- [LIKE (Transact-SQL)](../language-elements/like-transact-sql.md)
189+
- [CHARINDEX (Transact-SQL)](charindex-transact-sql.md)
190+
- [LEN (Transact-SQL)](len-transact-sql.md)
191+
- [Data types (Transact-SQL)](../data-types/data-types-transact-sql.md)
192+
- [String Functions (Transact-SQL)](string-functions-transact-sql.md)
193+
- [\[ \] (Wildcard - characters to match) (Transact-SQL)](../language-elements/wildcard-character-s-to-match-transact-sql.md)
194+
- [\[^\] (Wildcard - characters not to match) (Transact-SQL)](../language-elements/wildcard-character-s-not-to-match-transact-sql.md)
195+
- [_ (Wildcard - match one character) (Transact-SQL)](../language-elements/wildcard-match-one-character-transact-sql.md)
196+
- [Percent character (wildcard - characters to match) (Transact-SQL)](../language-elements/percent-character-wildcard-character-s-to-match-transact-sql.md)

0 commit comments

Comments
 (0)