You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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');
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;
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)
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
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!');
`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.
116
107
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):
119
113
120
114
```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;
`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;
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)
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 ASVARCHAR (10) ='safety';
172
+
173
+
SELECT PATINDEX('%'+ @MyValue +'%', DocumentSummary) AS position
[(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)
-[\[ \] (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