基于字符串值的函数 - string-length

适用范围:SQL Server

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

语法

  
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 2016 中数据库引擎功能的重大更改主题中的“XQuery 函数是代理项感知”部分。 另请参阅 ALTER DATABASE 兼容性级别(Transact-SQL)排序规则和 Unicode 支持

示例

本主题针对 AdventureWorks 数据库中各种 xml 类型列中存储的 XML 实例提供 XQuery 示例。

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 函数