varchar的数字顺序

时间:2018-10-02 14:30:27

标签: sql tsql numeric alphanumeric sql-order-by

嗨,我正在尝试对varchar列进行排序。我已经差不多完成了,但是我找不到找到以下解决方案的解决方案:

-999,-998、0、8A,80、80A,90,A80,ADBA

目前(参见代码): -998,-999、0、8A,80、80A,90,A80,ADBA

我只需要修复负值。任何人都可以解决这个问题吗?

 SELECT RANK_NUMBER FROM SHIPMENT_EVENT
    ORDER BY CASE WHEN ISNUMERIC(RANK_NUMBER) = 1 THEN RIGHT('0000000000' + RANK_NUMBER, 10) ELSE CASE 
                 WHEN LEFT(RANK_NUMBER, 1) LIKE '[0-9]' THEN RIGHT( 
                 '0000000000' 
                     + LEFT(SUBSTRING(RANK_NUMBER, PATINDEX('%[0-9.-]%', RANK_NUMBER 
                 ), 80 
                 ), 
                 PATINDEX 
                 ('%[^0-9.-]%', SUBSTRING(RANK_NUMBER, PATINDEX('%[0-9.-]%', 
                 RANK_NUMBER), 
                 80 
                 ) + 'X') -1), 10) 
                 ELSE RANK_NUMBER 
               END 
           END

1 个答案:

答案 0 :(得分:0)

我希望这样:

order by cast(left(col, patindex('%[^-0-9]%', col + 'x') - 1) as decimal(20, 0))