我有“ GUI_KVLevelName”列。 它的数据为:
500.00 / 69.00 / 34.50
500.00 / 400.00 / 138.00
500.00 / 69.00
500.00 / 400.00
500.00 / 345.00 / 34.50
57.00 / 8.30
我想与此一起使用订购。这是一个varchar列,但我想将其定为数字。那么我如何使用此列的订单?
答案 0 :(得分:2)
这可能有用一点作弊
通过删除小数点,我们将各个值转换为更大的INT。然后将其转换为hierarchyid
类型,然后进行排序
示例
Declare @YourTable Table ([GUI_KVLevelName] varchar(50))
Insert Into @YourTable Values
('500.00/69.00/34.50')
,('500.00/400.00/138.00')
,('500.00/69.00')
,('500.00/400.00')
,('500.00/345.00/34.50')
,('0.45/5.30') -- Added for leading zero
,('0.10/9.30') -- Added for leading zero
Select *
From @YourTable
Order By try_convert(hierarchyid,replace('/'+replace([GUI_KVLevelName],'.','1')+'/','/0','/'))
返回
GUI_KVLevelName
0.10/9.30
0.45/5.30
500.00/69.00
500.00/69.00/34.50
500.00/345.00/34.50
500.00/400.00
500.00/400.00/138.00
答案 1 :(得分:0)
第二个答案,以防您无法将GTD保留两位小数。
示例
Declare @YourTable Table ([GUI_KVLevelName] varchar(50))
Insert Into @YourTable Values
('500.00/69.00/34.50')
,('500.00/400.00/138.00')
,('500.00/69.00')
,('500.00/400.00')
,('500.00/345.00/34.50')
,('0.45/5.30')
,('0.1/9.30')
,('0.01/9.30')
,('0.05/9.30')
,('1.3/4.30')
Select A.*
From @YourTable A
Cross Apply (
Select Pos1 = xDim.value('/x[1]','money')
,Pos2 = xDim.value('/x[2]','money')
,Pos3 = xDim.value('/x[3]','money')
,Pos4 = xDim.value('/x[4]','money')
,Pos5 = xDim.value('/x[5]','money')
,Pos6 = xDim.value('/x[6]','money')
,Pos7 = xDim.value('/x[7]','money')
From (Select Cast('<x>' + replace([GUI_KVLevelName],'/','</x><x>')+'</x>' as xml) as xDim) as A
) B
Order By Pos1,Pos2,Pos3,Pos4,Pos5,Pos6,Pos7
返回
GUI_KVLevelName
0.01/9.30
0.05/9.30
0.1/9.30
0.45/5.30
1.3/4.30
500.00/69.00
500.00/69.00/34.50
500.00/345.00/34.50
500.00/400.00
500.00/400.00/138.00