SQL Server 2008 - order by strings with number numerically

前端 未结 5 1433
感动是毒
感动是毒 2020-12-01 16:06

I have following values in my table:

ABC
ABC1
ABC2
ABC3 and so on...

ABC11
ABC12
ABC13 and so on..

ABC20
ABC21
ABC22 and so on..

So basic

5条回答
  •  时光取名叫无心
    2020-12-01 16:52

    In order by statement, prepend enough zeros with when value contains any number in it to make all alphanumerica value same length

    SELECT ColName
    FROM TableName
    ORDER BY 
     CASE WHEN ColName like '%[0-9]%' 
     THEN Replicate('0', 100 - Len(ColName)) + ColName
     ELSE ColName  END
    

提交回复
热议问题