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
指定した式で最初に出現するパターンの開始位置を返します。パターンが見つからない場合は、すべての有効なテキストデータ型と文字データ型で 0 を返します。
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
.
式が 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 でパターンを使用する
次の例では、AdventureWorks2022 データベースの ensure
テーブルにある DocumentSummary
列の特定の行で、パターン Document
が始まる位置を検出します。
SELECT PATINDEX('%ensure%', DocumentSummary) AS position
FROM Production.Document
WHERE DocumentNode = 0x7B40;
GO
結果セットは次のとおりです。
position
--------
64
WHERE
句を使用して検索する行を制限しない場合、クエリはテーブル内のすべての行を返し、パターンが見つかった行の 0 以外の値を報告し、パターンが見つからなかったすべての行に対して 0 を報告します。
C. PATINDEX でワイルドカード文字を使用する
次の例では、ワイルドカードの % と _ を使用して、指定した文字列で任意の 1 文字と '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. PATINDEX で COLLATE を使用する
次の例では、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