Sql Server query varchar data sort like int

后端 未结 3 658
陌清茗
陌清茗 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:48

    Try this

    SELECT * FROM #varchar_field ORDER BY CASE WHEN ISNUMERIC(mixed_field) = 1 THEN CAST(mixed_field AS FLOAT) WHEN ISNUMERIC(LEFT(mixed_field,1)) = 0 THEN ASCII(LEFT(LOWER(mixed_field),1)) ELSE 2147483647 END

    Ref: http://sqlserverplanet.com/tsql/how-to-order-numeric-values-in-a-varchar-field

提交回复
热议问题