다음을 통해 공유


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

모든 유효한 텍스트 및 문자 데이터 형식에서 지정된 식에서 패턴이 처음 나타나는 시작 위치를 반환하거나 패턴을 찾을 수 없는 경우 0을 반환합니다.

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 면 오류를 반환합니다.

시작 위치 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. 자세한 내용은 데이터 정렬 및 유니코드 지원을 참조하십시오.

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에서 와일드카드 문자 사용

다음 예에서는 % 및 와일드카드를 사용하여 지정된 문자열에서 하나의 문자 및 'en'가 뒤에 오는 'ure' 패턴이 시작하는 위치를 찾습니다(인덱스가 1에서 시작함).

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

결과 집합은 다음과 같습니다.

position
--------
8

PATINDEXLIKE와 동일하게 작동하므로 와일드카드 중 하나를 사용할 수 있습니다. 백분율 사이에 패턴을 묶을 필요가 없습니다. 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