SQL Server 2008 - order by strings with number numerically

前端 未结 5 1436
感动是毒
感动是毒 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:58

    You could remove the first three characters and cast the rest to int

    SELECT Value,
           Num=CAST(RIGHT(Value, LEN(Value) - 3) AS int)
    FROM dbo.TableName
    ORDER BY Num
    

    Demo

提交回复
热议问题