【问题标题】:Convert hierarchyid to a string whose lexical order is the same as the hierarchyid depth-first order将hierarchyid转换为词法顺序与hierarchyid深度优先顺序相同的字符串
【发布时间】:2012-08-27 01:59:58
【问题描述】:

我想知道是否有一种合理、高效的方法可以将 hierarchyid 值转换为其词法顺序保持其自然深度优先顺序的字符串。

谢谢!

【问题讨论】:

    标签: sql-server sql-server-2008 tsql


    【解决方案1】:

    根据下面的代码(在 AdventureWorks2008 上运行),您可以使用样式 1 转换为 varbinary,然后转换为 nvarchar。如果没有样式 1,则无法正确排序。

    select convert(nvarchar(20),convert(varbinary(20),OrganizationNode,1),1)
            as OrderableString,
           OrganizationNode.ToString() as ReadableString,
           convert(nvarchar(20),OrganizationNode) as ReadableString2
    from humanresources.employee
    order by strNode;
    

    其中的一些排序给出了:

    OrderableString     ReadableString  ReadableString2
    0x7AD744    /3/1/1/8/   /3/1/1/8/
    0x7AD74C    /3/1/1/9/   /3/1/1/9/
    0x7AD754    /3/1/1/10/  /3/1/1/10/
    0x7AD75C    /3/1/1/11/  /3/1/1/11/
    0x7AD764    /3/1/1/12/  /3/1/1/12/
    0x7ADA      /3/1/2/     /3/1/2/
    0x7ADAB0    /3/1/2/1/   /3/1/2/1/
    0x7ADAD0    /3/1/2/2/   /3/1/2/2/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多