SQL> DESC AA;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
ID VARCHAR2(10) Y
NAME VARCHAR2(20) Y
SQL> SELECT * FROM AA;
ID NAME
---------- --------------------
100 aa
2 bb
3 cc
4 dd
1a ii
1b jj
1c kk
1e mm
10a xx
3b ww
2c vv
1d oo
希望得到如下结果:
ID NAME
---------- --------------------
2 bb
3 cc
4 dd
100 aa
1a ii
1b jj
1c kk
1d oo
1e mm
2c vv
3b ww
10a xx
SQL语句为:
select * from aa order by case when translate(id,'#1234567890','#') is null then 1 else 2 end, to_number(regexp_substr(id,'[[:digit:]]+')),id
如果此时向表中添加如下这条数据:insert into aa values('abs2','xx');此时再用上面的那条sql时,则结果不符合要求了。
此时的sql应该为:
select * from aa order by case when translate(id,'#1234567890','#') is null then 1 else 2 end, to_number(regexp_substr(id,'^[[:digit:]]+')),id
文中所用的SQL提供者为:wildwave (狂浪)