Skip to content

Latest commit

 

History

History
147 lines (107 loc) · 4.04 KB

File metadata and controls

147 lines (107 loc) · 4.04 KB
title string Function (XQuery)
description Learn about the XQuery function string() that returns the value of its argument represented as a string.
author rothja
ms.author jroth
ms.reviewer randolphwest
ms.date 09/26/2025
ms.service sql
ms.subservice xml
ms.topic reference
helpviewer_keywords
string function
fn:string function
dev_langs
XML

Data Accessor Functions - string (XQuery)

[!INCLUDE SQL Server Azure SQL Database]

Returns the value of $arg represented as a string.

Syntax

fn:string() as xs:string
fn:string($arg as item()?) as xs:string

Arguments

$arg

A node or an atomic value.

Remarks

  • If $arg is the empty sequence, the zero-length string is returned.

  • If $arg is a node, the function returns the string value of the node that is obtained by using the string-value accessor. This is defined in the W3C XQuery 1.0 and XPath 2.0 Data Model specification.

  • If $arg is an atomic value, the function returns the same string that is returned by the expression cast as xs:string, $arg, except when noted otherwise.

  • If the type of $arg is xs:anyURI, the URI is converted to a string without escaping special characters.

  • In this implementation, fn:string() without an argument can only be used in the context of a context-dependent predicate. Specifically, it can only be used inside brackets ([ ]).

Examples

This article provides XQuery examples against XML instances that are stored in various xml type columns in the AdventureWorks database.

A. Use the string function

The following query retrieves the <Features> child element node of the <ProductDescription> element.

SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
 /PD:ProductDescription/PD:Features
')
FROM Production.ProductModel
WHERE ProductModelID = 19;

Here's the partial result:

<PD:Features xmlns:PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
   These are the product highlights.
   <p1:Warranty xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
    <p1:WarrantyPeriod>3 years</p1:WarrantyPeriod>
    <p1:Description>parts and labor</p1:Description>
   </p1:Warranty>
       ...
</PD:Features>

If you specify the string() function, you receive the string value of the specified node.

SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
 string(/PD:ProductDescription[1]/PD:Features[1])
')
FROM Production.ProductModel
WHERE ProductModelID = 19;

Here's the partial result.

These are the product highlights.
3 yearsparts and labor...

B. Use the string function on various nodes

In the following example, an XML instance is assigned to an xml type variable. Queries are specified to illustrate the result of applying string() to various nodes.

DECLARE @x AS XML;

SET @x = '<?xml version="1.0" encoding="UTF-8" ?>
<!--  This is a comment -->
<root>
  <a>10</a>
just text
  <b attr="x">20</b>
</root>
';

The following query retrieves the string value of the document node. This value is formed by concatenating the string value of all its descendent text nodes.

SELECT @x.query('string(/)');

Here's the result:

This is a comment 10
just text
 20

The following query tries to retrieve the string value of a processing instruction node. The result is an empty sequence, because it doesn't contain a text node.

SELECT @x.query('string(/processing-instruction()[1])');

The following query retrieves the string value of the comment node and returns the text node.

SELECT @x.query('string(/comment()[1])');

Here's the result:

This is a comment

Related content