开发者

Confusion in order by clause in SQL server

I want to use order by clause to return 'id' column value of my table in sorted order like '1,2,3,4..' but the the following query returning value of id in the order '1, 10, 100, 101, 102...'

select id from loginuser order by id;

May I know can I get my desirable output?开发者_如何学JAVA


Your id column is of type varchar, try casting to int in the order by clause to get the desired result.


if possible you should change the column type in your database.


Adding to @Greco's answer, it is good practise to expose the sort order to the caller. Anyhow, using an expression in the ORDER BY clause violates SQL Standards. Therefore, consider this alternative:

SELECT id, CAST(id AS INTEGER) AS sort_col  
  FROM loginuser 
 ORDER 
    BY sort_col;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜