使用sql查询获得正确的升序

时间:2013-10-29 05:25:40

标签: sql

我的SQL查询是..

Select left(Bundle_code,2)
From Item_bundle 
where Item_code='F-4X10AL' and Branch ='KOCHI'
Order by CAST ( Case When ISNUMERIC (left(Bundle_code,2))=1 
                     Then left( Bundle_code,2) 
                     Else -99 End as Int)

我得到的结果是

result:
2B
6B
3B
7B
8B
9B
10
11

但订单不正确。请帮我订一个正确的订单

1 个答案:

答案 0 :(得分:0)

假设您希望Bundle_code为int(1,2 ..)+'B',您可以写为:

Select  SUBSTRING(Bundle_code,0,CHARINDEX('B',Bundle_code,0) + 1) as Bundle_code
From Item_bundle where Item_code='F-4X10AL' and Branch ='KOCHI'
Order by cast(SUBSTRING(Bundle_code,0,CHARINDEX('B',Bundle_code,0)) as int)