Oracle sql命令按字符顺序排列

时间:2012-08-20 07:24:25

标签: sql oracle select sql-order-by

美好的一天,也许有人可以帮助我,我想在oracle sql上获得n选择输出,但是当我按顺序排序时它是不正确的。

code_order
1.
2.
2.1
3.
4.2
10.1
10.0
21.
21.1
23.
31.

它需要按数字排序,所有值都在.

中有句号

任何想法??

Thanx,但我发现某些值可以包含非数字值,如C,B等。

                    1. 7C。 40. 50. 51. 6。
  1. 表格大纲:

    code_order是varchar2

2 个答案:

答案 0 :(得分:0)

使用order by to_number(code_order)

答案 1 :(得分:0)

    with
    val as
    (
      SELECT '1.' as c FROM dual
      union all
      SELECT '2.' as c FROM dual
      union all
      SELECT '3.' as c FROM dual
      union all
      SELECT '2.1' as c FROM dual
      union all
      SELECT '4.2' as c FROM dual
      union all
      SELECT '10.1' as c FROM dual
      union all
      SELECT '21.' as c FROM dual
      union all
      SELECT '10.0' as c FROM dual
      union all
      SELECT '21.1' as c FROM dual
      union all
      SELECT '23.' as c FROM dual
      union all
      SELECT '31.' as c FROM dual
    )
SELECT c FROM val
order by to_number(regexp_replace(c, '^(\d+)\..*$', '\1'))
    ;