PATINDEX (Transact-SQL)

Applies to:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)SQL analytics endpoint in Microsoft FabricWarehouse in Microsoft Fabric

返回指定表达式中模式的第一个匹配项的起始位置;如果未找到模式,则返回所有有效文本和字符数据类型上的零。

Transact-SQL 语法约定

Syntax

PATINDEX ( '%pattern%' , expression )

Arguments

pattern

包含要查找的序列的字符表达式。 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.

Note

虽然 SQL Server 2022 (16.x) 和早期版本中不支持传统正则表达式,但可以使用各种通配符表达式实现类似的复杂模式匹配。 See the String operators documentation for more detail on wildcard syntax. 有关 SQL Server 2025 (17.x) 预览版中的正则表达式函数的信息,请参阅 正则表达式函数

expression

An expression, typically a column that is searched for the specified pattern. expression is of the character string data type category.

Return types

bigint if expression is of the varchar(max) or nvarchar(max) data types; otherwise int.

Remarks

If pattern is NULL, PATINDEX returns NULL.

如果表达式为 NULLPATINDEX 则返回错误。

的起始位置 PATINDEX1

PATINDEX 根据输入的排序规则执行比较。 若要在指定的排序规则中执行比较,可以使用 COLLATE 该排序规则将显式排序规则应用于输入。

补充字符(代理项对)

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. 有关详细信息,请参阅 排序规则和 Unicode 支持

0x0000 (char(0)) is an undefined character in Windows collations and can't be included in PATINDEX.

Examples

A. 基本 PATINDEX 示例

以下示例检查字符 interesting data 起始位置的短字符串 (ter)。

SELECT PATINDEX('%ter%', 'interesting data') AS position;

结果集如下。

position
--------
3

B. 将模式与 PATINDEX 配合使用

以下示例查找模式 ensure 在 AdventureWorks2022 数据库的 DocumentSummary 表中 Document 列特定行中的开始位置。

SELECT PATINDEX('%ensure%', DocumentSummary) AS position
FROM Production.Document
WHERE DocumentNode = 0x7B40;
GO

结果集如下。

position
--------
64

如果不限制使用 WHERE 子句搜索的行,查询将返回表中的所有行,并报告在其中找到模式的行的非零值,对于未找到模式的所有行,则返回零。

C. 将通配符与 PATINDEX 配合使用

以下示例使用 % 和 _ 通配符查找模式 'en'(后跟任意一个字符和 'ure')在指定字符串中的开始位置(索引从 1 开始):

SELECT PATINDEX('%en_ure%', 'Please ensure the door is locked!') AS position;

结果集如下。

position
--------
8

PATINDEX 的作用与 LIKE 类似,因此,您可以使用任何通配符。 不必将模式括在百分比之间。 PATINDEX('a%', 'abc') 将返回 1;PATINDEX('%a', 'cba') 将返回 3。

LIKE 不同的是,PATINDEX 返回一个位置,这与 CHARINDEX 类似。

D. 将复杂的通配符表达式与 PATINDEX 配合使用

The following example uses the [^]string operator to find the position of a character that isn't a number, letter, or space.

SELECT PATINDEX('%[^ 0-9A-Za-z]%', 'Please ensure the door is locked!') AS position;

结果集如下。

position
--------
33

E. 将 COLLATE 与 PATINDEX 配合使用

以下示例使用 COLLATE 函数显式指定要搜索的表达式的排序规则。

USE tempdb;
GO

SELECT PATINDEX('%ein%', 'Das ist ein Test' COLLATE Latin1_General_BIN);
GO

结果集如下。

position
--------
9

F. 使用变量指定模式

The following example uses a variable to pass a value to the pattern parameter. 该示例使用 AdventureWorks2022 数据库。

DECLARE @MyValue AS VARCHAR (10) = 'safety';

SELECT PATINDEX('%' + @MyValue + '%', DocumentSummary) AS position
FROM Production.Document
WHERE DocumentNode = 0x7B40;

结果集如下。

position
--------
22