首页 > 解决方案 > SQL ORACLE 按数字和字符串排序

问题描述

我尝试使用:

select * from list order by  id;
select * from list order by  LPAD(id, 4);

但它不起作用。

我在 oracle 表中有这个列表:

id (varchar2)
-----------
123
124
125
126
toto
bobo
koko
201
169

所以我需要这样的结果:

123
124
125
126
169
201
bobo
koko
toto

标签: sqloracle

解决方案


我会建议:

order by (case when regexp_like(id, '^[0-9]+$')
               then to_number(id)
          end) asc nulls last,
         id

这将以数字方式处理数字 id,即使它们的长度不同。


推荐阅读