简体   繁体   English

Oracle 10g-用字母对数字排序

[英]Oracle 10g - Sort numbers with letters

I need to sort the following list of values, dots included: 我需要对以下值列表进行排序,包括点:

1.
10.
2f.
2c.
2a.

I need them sorted in the following order: 我需要按以下顺序对其进行排序:

1.
2a.
2c.
2f.
10.

I used the following code on SQL Developer to sort the list: 我在SQL Developer上使用以下代码对列表进行排序:

with testdata as
(
select column_value from table (sys.odcivarchar2list
   ('1. '
   ,'10. '
   ,'2f. '
   ,'2b. '
   ,'2a. '))
   )
select column_value
  from testdata
 order by case when replace(translate(trim(column_value),'0123456789','0'),'0','') is null then to_number(column_value) end
         ,column_value
/

but I did not get the result I wanted: 但是我没有得到想要的结果:

1.
10.
2a.
2c.
2f.

Thank you in advance for your help. 预先感谢您的帮助。

You can use regexp_substr to order by numbers in the string first and then by the remaining non-numeric characters. 您可以使用regexp_substr字符串中的数字排序,然后再按其余的非数字字符排序。 This assumes the string always has number(s) followed by non-numeric character(s). 这假定字符串始终具有数字,后跟非数字字符。

select column_value
from testdata
order by cast(regexp_substr(column_value,'[0-9]+') as int), 
regexp_substr(column_value,'[^0-9]+')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM