Skip to content

Latest commit

 

History

History
55 lines (45 loc) · 1.84 KB

string-search-in-xquery.md

File metadata and controls

55 lines (45 loc) · 1.84 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic helpviewer_keywords dev_langs
String Search in XQuery
Learn how to search text in XML documents by viewing an example of string search in XQuery.
rothja
jroth
03/04/2017
sql
xml
reference
strings [SQL Server], search
XML [SQL Server], searching text
searches [SQL Server], XML documents
XQuery, string search
XML

String Search in XQuery

[!INCLUDE SQL Server Azure SQL Database]

This topic provides sample queries that show how to search text in XML documents.

Examples

A. Find feature descriptions that contain the word "maintenance" in the product catalog

SELECT CatalogDescription.query('  
     declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
    for $f in /p1:ProductDescription/p1:Features/*  
     where contains(string($f), "maintenance")  
     return  
           $f ') as Result  
FROM Production.ProductModel  
WHERE ProductModelID=19  

In the previous query, the where in the FLOWR expression filters the result of the for expression and returns only elements that satisfy the contains() condition.

This is the result:

<p1:Maintenance     
      xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">  
 <p1:NoOfYears>10</p1:NoOfYears>  
 <p1:Description>maintenance contact available through your   
               dealer or any AdventureWorks retail store.</p1:Description>  
</p1:Maintenance>  

See Also

XML Data (SQL Server)
XQuery Language Reference (SQL Server)