contains 함수(XQuery)

$arg1 값에 $arg2로 지정된 문자열 값이 포함되었는지 여부를 나타내는 xs:boolean 유형의 값을 반환합니다.

구문

fn:contains ($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean?

인수

  • $arg1
    테스트할 문자열 값입니다.

  • $arg2
    검색할 하위 문자열입니다.

주의

$arg2 값이 길이가 0인 문자열인 경우 함수는 True를 반환합니다. $arg1 값이 길이가 0인 문자열이고 $arg2 값이 길이가 0이 아닌 문자열이면 함수가 False를 반환합니다.

$arg1 또는 $arg2 값이 빈 시퀀스인 경우 인수는 길이가 0인 문자열로 취급됩니다.

contains() 함수는 문자열 비교를 위해 XQuery의 기본 유니코드 코드 포인트 데이터 정렬을 사용합니다.

$arg2에 대해 지정된 하위 문자열 값은 4000자보다 작거나 같아야 합니다. 지정된 값이 4000자보다 큰 경우 동적 오류 조건이 발생하고 contains() 함수는 True 또는 False의 부울 값 대신 빈 시퀀스를 반환합니다. SQL Server는 XQuery 식에서 동적 오류를 발생시키지 않습니다.

비교 시 대/소문자를 구분하지 않으려면 upper-case 또는 lower-case 함수를 사용할 수 있습니다.

보조 문자(서로게이트 쌍)

XQuery 함수에서 서로게이트 쌍의 동작은 데이터베이스 호환성 수준과 일부 경우 함수의 기본 네임스페이스 URI에 따라 달라집니다. 자세한 내용은 SQL Server 2012 데이터베이스 엔진 기능의 주요 변경 항목의 "서로게이트 인식 XQuery 함수" 섹션을 참조하십시오. 또한 ALTER DATABASE 호환성 수준(Transact-SQL)데이터 정렬 및 유니코드 지원를 참조하십시오.

이 항목에서는 AdventureWorks 데이터베이스의 다양한 xml 유형 열에 저장된 XML 인스턴스에 대한 XQuery 예를 제공합니다.

1.contains() XQuery 함수를 사용하여 특정 문자열 검색

다음 쿼리는 요약 설명에 Aerodynamic이라는 단어가 포함된 제품을 검색합니다. 이 쿼리는 이러한 제품에 대한 ProductID와 <Summary> 요소를 반환합니다.

--The product model description document uses
--namespaces. The WHERE clause uses the exit()
--method of the xml data type. Inside the exit method,
--the XQuery contains()function is used to
--determine whether the <Summary> text contains the word
--Aerodynamic. 

USE AdventureWorks
GO
WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd)
SELECT ProductModelID, CatalogDescription.query('
      <Prod>
         { /pd:ProductDescription/@ProductModelID }
         { /pd:ProductDescription/pd:Summary }
      </Prod>
 ') as Result
FROM Production.ProductModel
where CatalogDescription.exist('
   /pd:ProductDescription/pd:Summary//text()
    [contains(., "Aerodynamic")]') = 1

결과

ProductModelID Result

-------------- ---------

28 <Prod ProductModelID="28">

<pd:Summary xmlns:pd=

"https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">

<p1:p xmlns:p1="http://www.w3.org/1999/xhtml">

A TRUE multi-sport bike that offers streamlined riding and

a revolutionary design. Aerodynamic design lets you ride with

the pros, and the gearing will conquer hilly roads.</p1:p>

</pd:Summary>

</Prod>

참고 항목

참조

xml 데이터 형식에 대한 XQuery 함수