string-length 函数 (XQuery)

返回字符串的长度(以字符为单位)。

语法

fn:string-length() as xs:integer
fn:string-length($arg as xs:string?) as xs:integer

参数

  • $arg
    要计算其长度的源字符串。

注释

如果 $arg 的值是空序列,则返回 xs:integer 的值为 0。

XQuery 函数中代理对的行为依赖于数据库兼容级别。 如果该兼容级别为 110 或更高,则每个代理对都作为单个字符计数。 对于更低的兼容级别,它们会作为两个字符计数。 有关详细信息,请参阅 ALTER DATABASE 兼容级别 (Transact-SQL)排序规则和 Unicode 支持

如果该值包含由两个代理项字符表示的 4 字节 Unicode 字符,则 SQL Server 将单独对代理项字符计数。

不带参数的 string-length() 只能在谓词内使用。 例如,下面的查询返回 <ROOT> 元素:

DECLARE @x xml;
SET @x='<ROOT>Hello</ROOT>';
SELECT @x.query('/ROOT[string-length()=5]');

补充字符(代理项对)

XQuery 函数中代理对的行为依赖于数据库兼容级别,并且在某些情况下,还依赖于函数的默认命名空间 URI。 有关详细信息,请参阅主题SQL Server 2012 中数据库引擎功能的重大更改中的“XQuery 函数可识别代理”部分。 另请参阅 ALTER DATABASE 兼容级别 (Transact-SQL)排序规则和 Unicode 支持

示例

本主题提供了一些针对 XML 实例的 XQuery 示例,这些实例存储在 AdventureWorks 数据库内不同的 xml 类型列中。

A.使用 string-length() XQuery 函数检索带有较长摘要说明的产品

对于摘要说明大于 50 个字符的产品,下面的查询将检索产品 ID、摘要说明的长度以及摘要本身(即 <Summary> 元素)。

WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' as pd)
SELECT CatalogDescription.query('
      <Prod ProductID= "{ /pd:ProductDescription[1]/@ProductModelID }" >
       <LongSummary SummaryLength = 
           "{string-length(string( (/pd:ProductDescription/pd:Summary)[1] )) }" >
           { string( (/pd:ProductDescription/pd:Summary)[1] ) }
       </LongSummary>
      </Prod>
 ') as Result
FROM Production.ProductModel
WHERE CatalogDescription.value('string-length( string( (/pd:ProductDescription/pd:Summary)[1]))', 'decimal') > 200;

注意上述查询的以下方面:

下面是部分结果:

Result
-------------------
<Prod ProductID="19">
      <LongSummary SummaryLength="214">Our top-of-the-line competition 
             mountain bike. Performance-enhancing options include the
             innovative HL Frame, super-smooth front suspension, and 
             traction for all terrain.
      </LongSummary>
</Prod>
...

B.使用 string-length() XQuery 函数检索保修说明较短的产品

对于保修说明少于 20 个字符的产品,下面的查询将检索包含产品 ID、长度、保修说明以及 <Warranty> 元素本身的 XML。

保修是厂商为产品提供的服务之一。 可选的 <Warranty> 子元素位于 <Features> 元素后面。

WITH XMLNAMESPACES (
'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd,
'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain' AS wm)

SELECT CatalogDescription.query('
      for   $ProdDesc in /pd:ProductDescription,
            $pf in $ProdDesc/pd:Features/wm:Warranty
      where string-length( string(($pf/wm:Description)[1]) ) < 20
      return 
          <Prod >
             { $ProdDesc/@ProductModelID }
             <ShortFeature FeatureDescLength = 
                             "{string-length( string(($pf/wm:Description)[1]) ) }" >
                 { $pf }
             </ShortFeature>
          </Prod>
     ') as Result
FROM Production.ProductModel
WHERE CatalogDescription.exist('/pd:ProductDescription')=1;

请注意上述查询的以下方面:

  • pdwm 是用于此查询的命名空间前缀。 它们标识正在查询的文档中使用的相同命名空间。

  • XQuery 指定嵌套 FOR 循环。 外部 FOR 循环是必需的,因为要检索 <ProductDescription> 元素的 ProductModelID 属性。 内部 FOR 循环也是必需的,因为您只需检索那些保修功能说明少于 20 个字符的产品。

下面是部分结果:

Result
-------------------------
<Prod ProductModelID="19">
  <ShortFeature FeatureDescLength="15">
    <wm:Warranty 
       xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
      <wm:WarrantyPeriod>3 years</wm:WarrantyPeriod>
      <wm:Description>parts and labor</wm:Description>
    </wm:Warranty>
   </ShortFeature>
</Prod>
...

请参阅

参考

针对 xml 数据类型的 XQuery 函数