请教一个oracle排序的问题,现有一个order字段为varchar类型,其数据格式为:
1
2.1
2.1.2
5.1
10
10.1
20
默认的order只是安装字符串的格式进行排序
1
10
10.1
20
2.1
2.1.2
5.1
希望能够按照数字的大小进行排序,不知有没有一种简单的方法

解决方案 »

  1.   


    SQL> 
    SQL> create table test(id varchar(30));
    Table created
    SQL> begin
      2      insert into test values('1');
      3      insert into test values('2.1');
      4      insert into test values('2.1.2');
      5      insert into test values('2.1.1');
      6      insert into test values('5.1');
      7      insert into test values('10');
      8      insert into test values('10.1');
      9      insert into test values('20');
     10  end;
     11  /
    PL/SQL procedure successfully completed
    SQL> select id
      2  from test t
      3  order by
      4      0 + nvl(regexp_substr(id,'[^.]+',1,1),0),
      5      0 + nvl(regexp_substr(id,'[^.]+',1,2),0),
      6      0 + nvl(regexp_substr(id,'[^.]+',1,3),0),
      7      0 + nvl(regexp_substr(id,'[^.]+',1,4),0)
      8      ;
    ID
    ------------------------------
    1
    2.1
    2.1.1
    2.1.2
    5.1
    10
    10.1
    20
    8 rows selected
    SQL> drop table test purge;
    Table droppedSQL> 
      

  2.   

    借用版主的数据SQL>      select t.id from test t order by to_number(regexp_replace(t.id,'\.','',1,2));ID
    ------------------------------
    1
    2.1
    2.1.1
    2.1.2
    5.1
    10
    10.1
    20