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 の既定の Unicode コード ポイントの照合順序が使用されます。

$arg2 に指定するサブストリングの値は、4,000 文字以下にする必要があります。指定した値が 4,000 文字を超える場合、動的なエラー状態が発生し、contains() 関数は True または False のブール値ではなく空のシーケンスを返します。SQL Server では XQuery 式に対して動的なエラーは発生しません。

大文字と小文字の比較を取得するには、upper-case 関数または lower-case 関数を使用できます。

このトピックでは、AdventureWorks データベースのさまざまな xml 型列に格納されている XML インスタンスに対して実行する XQuery の例を紹介します。これらの各列の概要については、「AdventureWorks データベースの xml データ型表現」を参照してください。

A. 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 の結果

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

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>