Sql Server query varchar data sort like int

后端 未结 3 664
陌清茗
陌清茗 2021-02-05 10:16

I have one table like

CREATE TABLE table_name
(
P_Id int,
amount varchar(50)
)

Data Like

Id amount
----------
1 2340
2 4568
3         


        
3条回答
  •  你的背包
    2021-02-05 10:58

    Cast amount column into Numeric in ORDER BY clause while selecting:

    SELECT * FROM MyTable
    ORDER BY CAST(amount AS Numeric(10,0)) DESC
    

    Result:

    Id amount
    3 10000
    2 4568
    1 2340

    See this SQLFiddle

提交回复
热议问题