Funzione contains (XQuery)

Restituisce un valore di tipo xs:boolean che indica se il valore di $arg1 contiene un valore stringa specificato da $arg2.

Sintassi

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

Argomenti

  • $arg1
    Valore stringa da testare.

  • $arg2
    Sottostringa da cercare.

Osservazioni

Se il valore di $arg2 è una stringa di lunghezza zero, la funzione restituisce True. Se il valore di $arg1 è una stringa di lunghezza zero e il valore di $arg2 non è una stringa di lunghezza zero, la funzione restituisce False.

Se il valore di $arg1 o di $arg2 è la sequenza vuota, l'argomento viene considerato come la stringa di lunghezza zero.

La funzione contains() utilizza le regole di confronto dei punti di codice Unicode predefinite di XQuery per il confronto delle stringhe.

Il valore di sottostringa specificato per $arg2 deve essere inferiore o uguale a 4000 caratteri. Se il valore specificato è maggiore di 4000 caratteri, viene generato un errore dinamico e la funzione contains() restituisce una sequenza vuota anziché un valore booleano True o False. In SQL Server non vengono generati errori dinamici nelle espressioni XQuery.

Per ottenere confronti senza distinzione tra maiuscole e minuscole, è possibile utilizzare le funzioni upper-case o lower-case.

Esempi

In questo argomento vengono forniti esempi di utilizzo del linguaggio XQuery sulle istanze XML archiviate in diverse colonne di tipo xml- nel database AdventureWorks2008R2. Per una panoramica su ognuna di queste colonne, vedere Rappresentazione del tipo di dati XML nel database AdventureWorks2008R2.

A. Utilizzo della funzione XQuery contains() per cercare una stringa di caratteri specifica

La query seguente trova i prodotti per i quali la descrizione di riepilogo contiene la parola Aerodynamic. La query restituisce il valore ProductID e l'elemento <Summary> relativi a tali prodotti.

--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 AdventureWorks2008R2;
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;

Results

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>

Vedere anche

Riferimento