2013-10-29 97 views
-3

我的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 

但順序是不正確的。請幫我得到一個正確的訂購

+0

使用命令後的情況下 –

+0

我需要的訂單是2B 3B 6B 7B 8B 9B等等 – user2837585

+0

是10,11你想考慮還是10B,11B? – Deepshikha

回答

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)