Applies to:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
SQL analytics endpoint in Microsoft Fabric
Warehouse in Microsoft Fabric
在所有有效的文字和字元資料類型上,傳回指定運算式中第一次出現模式的起始位置,如果找不到模式,則為零。
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.
如果 expression 是 NULL, PATINDEX 則傳回錯誤。
的 PATINDEX 起始位置是 1。
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 資料庫中數據表中數據行的特定數據列中DocumentSummaryDocument開始的位置。
SELECT PATINDEX('%ensure%', DocumentSummary) AS position
FROM Production.Document
WHERE DocumentNode = 0x7B40;
GO
結果集如下所示。
position
--------
64
如果您未使用子句來 WHERE 限制要搜尋的資料列,則查詢會傳回資料表中的所有資料列,並報告找到型樣的資料列的非零值,並報告找不到型樣的所有資料列的零值。
C. 搭配 PATINDEX 使用萬用字元
下列範例在指定的字串中 (索引從 1 開始),使用 % 和 _ 萬用字元來尋找模式 'en' 後面接著任何一個字元和 'ure' 的開始位置:
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