Skip to content

Commit 3e99674

Browse files
committed
Freshness pass
1 parent ff050b7 commit 3e99674

File tree

1 file changed

+141
-126
lines changed

1 file changed

+141
-126
lines changed
Lines changed: 141 additions & 126 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,164 +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 *pattern* is NULL, PATINDEX returns NULL.
51-
52-
If expression is NULL, PATINDEX returns error.
53-
54-
The starting position for PATINDEX is 1.
55-
56-
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.
57-
58-
## Supplementary Characters (Surrogate Pairs)
59-
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).
60-
61-
0x0000 (**char(0)**) is an undefined character in Windows collations and cannot be included in PATINDEX.
62-
63-
## Examples
64-
65-
### A. Simple PATINDEX example
66-
The following example checks a short character string (`interesting data`) for the starting location of the characters `ter`.
67-
68-
```sql
69-
SELECT position = PATINDEX('%ter%', 'interesting data');
70-
```
71-
72-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
7339

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;
7479
```
80+
81+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
82+
83+
```output
7584
position
7685
--------
7786
3
7887
```
79-
80-
### B. Using a pattern with PATINDEX
81-
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.
82-
83-
```sql
84-
SELECT position = PATINDEX('%ensure%',DocumentSummary)
85-
FROM Production.Document
86-
WHERE DocumentNode = 0x7B40;
87-
GO
88-
```
89-
90-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
91-
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
9298
```
99+
100+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
101+
102+
```output
93103
position
94-
--------
95-
64
96-
```
97-
98-
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.
99-
100-
### C. Using wildcard characters with PATINDEX
101-
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):
102-
103-
```sql
104-
SELECT position = PATINDEX('%en_ure%', 'Please ensure the door is locked!');
105-
```
106-
107-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
108-
104+
--------
105+
64
109106
```
110-
position
111-
--------
112-
8
113-
```
114-
115-
`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.
116-
117-
Unlike `LIKE`, `PATINDEX` returns a position, similar to what `CHARINDEX` does.
118107

119-
### D. Using complex wildcard expressions with PATINDEX
120-
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):
121113

122114
```sql
123-
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
124124
```
125-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
126125

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;
127136
```
137+
138+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
139+
140+
```output
128141
position
129142
--------
130143
33
131144
```
132145

133-
### E. Using COLLATE with PATINDEX
134-
The following example uses the `COLLATE` function to explicitly specify the collation of the expression that is searched.
135-
136-
```sql
137-
USE tempdb;
138-
GO
139-
SELECT PATINDEX ( '%ein%', 'Das ist ein Test' COLLATE Latin1_General_BIN) ;
140-
GO
141-
```
142-
[!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
143153

154+
SELECT PATINDEX('%ein%', 'Das ist ein Test' COLLATE Latin1_General_BIN);
155+
GO
144156
```
157+
158+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
159+
160+
```output
145161
position
146162
--------
147163
9
148164
```
149165

150-
### F. Using a variable to specify the pattern
151-
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.
152-
153-
```sql
154-
DECLARE @MyValue VARCHAR(10) = 'safety';
155-
SELECT position = PATINDEX('%' + @MyValue + '%', DocumentSummary)
156-
FROM Production.Document
157-
WHERE DocumentNode = 0x7B40;
158-
```
159-
160-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
161-
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;
162176
```
177+
178+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
179+
180+
```output
163181
position
164-
--------
182+
--------
165183
22
166-
```
167-
168-
## See Also
169-
[LIKE (Transact-SQL)](../../t-sql/language-elements/like-transact-sql.md)
170-
[CHARINDEX (Transact-SQL)](../../t-sql/functions/charindex-transact-sql.md)
171-
[LEN (Transact-SQL)](../../t-sql/functions/len-transact-sql.md)
172-
[Data Types (Transact-SQL)](../../t-sql/data-types/data-types-transact-sql.md)
173-
[String Functions (Transact-SQL)](../../t-sql/functions/string-functions-transact-sql.md)
174-
[(Wildcard - Character(s) to Match) (Transact-SQL)](../../t-sql/language-elements/wildcard-character-s-to-match-transact-sql.md)
175-
[(Wildcard - Character(s) Not to Match) (Transact-SQL)](../../t-sql/language-elements/wildcard-character-s-not-to-match-transact-sql.md)
176-
[_ (Wildcard - Match One Character) (Transact-SQL)](../../t-sql/language-elements/wildcard-match-one-character-transact-sql.md)
177-
[Percent character (Wildcard - Character(s) to Match) (Transact-SQL)](../../t-sql/language-elements/percent-character-wildcard-character-s-to-match-transact-sql.md)
178-
179-
184+
```
180185

186+
## Related content
181187

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)