如何将hierarchyid转换为int [英] How cast hierarchyid to int

查看:77
本文介绍了如何将hierarchyid转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过asc命令hierarchyid字段,但我不知道如何将它转换为int

I want order hierarchyid field by asc, but i dont know how cast it to int

select Path.ToString() as Nomer ,
        Path,
        Name,
        Path.GetLevel() as Axixa

from Users order by cast(Path as nvarchar(100)) ASC;



/1/1/
/1/2/
/1/3/
/2/1/
/2/2/
/2/3/
-/214/1/-
-/214/2/-
/3/1/1/
/3/1/2/

should be so

/1/1/
/1/2/
/1/3/
/2/1/
/2/2/
/2/3/
/3/1/1/
/3/1/2/
/214/1/
/214/2/

推荐答案

无需转换列 - 只需按 [路径] 订购, HierarchyID 类型将做正确的事:

No need to convert the column - just order by [Path], and the HierarchyID type will do the right thing:
SELECT
   Path.ToString() as Nomer,
   Path,
   Name,
   Path.GetLevel() as Axixa
FROM
   Users
ORDER BY
   Path
;

/*
Output:

/1/1/
/1/2/
/1/3/
/2/1/
/2/2/
/2/3/
/3/1/1/
/3/1/2/
/214/1/
/214/2/
*/



http://sqlfiddle.com/#!6/08c3e/1 [ ^ ]


您可以按顺序使用函数来帮助。这里假设您的最大路径是3个字符。如果你更喜欢添加更多0并将3改为0的数量。



祝你好运



You can use functions in the order by to help out. Here it assumes your largest path is 3 characters. If you like more add more 0's and change the 3's to the number of 0's.

Good Luck

ORDER BY RIGHT('000'+CAST(Path AS VARCHAR(3)),3) ASC


这篇关于如何将hierarchyid转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆