我有一些像下面这样的孩子
childid
------------
1.1
1.2
2.8
2.7
6.5
6.5.1
6.5.15
7.1
8
排序顺序将是
childid
--------
1.1
1.2
2.7
2.8
6.5
6.51
6.5.15
7.1
我尝试转换为整数,如下所示
declare @str nvarchar(max)='1.23.2';
set @str=(select replace(@str,'.',''))
select @str
但它失败时
7.1
8
来了
它给出的顺序如下
8
7.1
但我需要像下面这样的订单
7.1
8
另外,如果数字像
7.1.1
7.1.8
6.7.7.7
那么顺序应该是
6.7.7.7
7.1.1
7.1.8
我希望有人能帮助我解决这个问题
试试这样:
编辑:我也更改了处理“123abc”等非数字值的方法。
declare @ids table(idList varchar(100))
insert into @ids values
('1.1')
,('1.2')
,('2.8')
,('2.7')
,('6.5')
,('6.5.1')
,('6.5.15')
,('7.1')
,('8');
select idList,padded.OrderBy
from @ids as ids
cross apply(select cast('<r>' + replace(idList,'.','</r><r>') + '</r>' as xml)) as AsXml(val)
cross apply
(
select right(' ' + rtrim(x.y.value('.','varchar(max)')),10)
from AsXml.val.nodes('/r') as x(y)
for xml path('')
) as padded(OrderBy)
order by padded.OrderBy
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)