適用対象:SQL Server
$arg 1 の値に $arg 2 で指定された文字列値が含まれているかどうかを示す xs:boolean 型の値を返します。
構文
fn:contains ($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean?
引数
$arg1
テストする文字列値。
$arg2
検索するサブストリング。
解説
$arg 2 の値が長さ 0 の文字列の場合、関数は True を返します。 $arg 1 の値が長さ 0 の文字列で、$arg 2 の値が長さ 0 の文字列でない場合、関数は False を返します。
$arg 1 または $arg 2 の値が空のシーケンスの場合、引数は長さ 0 の文字列として扱われます。
contains() 関数では、文字列の比較に XQuery の既定の Unicode コード ポイントの照合順序が使用されます。
$arg 2 に指定する部分文字列の値は、4,000 文字以下である必要があります。 指定した値が 4,000 文字を超える場合、動的エラー状態が発生し、contains() 関数はブール値 True または False の代わりに空のシーケンスを返します。 SQL Server では、XQuery 式で動的エラーは発生しません。
大文字と小文字を区別しない比較を取得するために、 大文字と小文字 または小文字の関数を使用できます。
補助文字 (サロゲート ペア)
XQuery 関数でのサロゲート ペアの動作は、データベース互換性レベルと、場合によっては関数の既定の名前空間 URI によって異なります。 詳細については、ALTER DATABASE)およびコレーションおよびUnicodeサポートをご覧ください。
例
このトピックでは、AdventureWorks データベースのさまざまな xml 型列に格納されている XML インスタンスに対する XQuery の例を示します。
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 AdventureWorks2022;
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>