Oracle按数字和alpha排序,例如:1、1a,1b,2、2c

时间:2019-04-29 13:25:43

标签: oracle sql-order-by

我有此数据:

1
10
100
101
102
12
120
1a
1b
1c
2
3
4

,我想这样订购:

1
1a
1b
1c
2
3
4
10
12
100
101
102
120

在Oracle中有可能吗?

我尝试过此操作,但不适用于字母字符

order by case when replace(translate(trim(COLUMN),'0123456789','0'),'0','') is null then to_number(COLUMN) end asc, COLUMN asc

1 个答案:

答案 0 :(得分:0)

如果您不介意性能下降,那么使用regexp是一个简单的解决方案。

-- sample data
with t as (select '1' as x from dual
            union select '10' from dual
            union select '100' from dual
            union select '101' from dual
            union select '102' from dual
            union select '12' from dual
            union select '120' from dual
            union select '1a' from dual
            union select '1b' from dual
            union select '1c' from dual
            union select '2' from dual
            union select '3' from dual
            union select '4' from dual)
-- query
select x
from t
-- order first by the leading numbers, then alphabetically
order by to_number(regexp_substr(x, '[0-9]*')), x;