0

当我使用以下查询时

select model from client_source order by model;

结果数据按此顺序

model
______
1234
4g
default
unknown
default1
ipad
iphone simulator
ipod touch
iphone
ipod
test

但我希望按以下顺序排序

model
________

1234
4g
default
default1
ipad
iphone
iphone simulator
ipod
ipod touch
test
unknown
4

1 回答 1

2

我想你的NLS_SORT参数是BINARY. 在BINARY排序中,字符串按照它们的二进制表达式排序,这意味着大写字母将排在小写字母之前:

SQL> alter session set nls_sort=binary;

Session altered

SQL> select model from client_source order by model;

MODEL
--------------------
1234
4g
Default
Unknown
default1
ipad
iphone
iphone simulator
ipod
ipod touch
test

NLS_SORT您可以使用不同的值更改此行为。使用带后缀的NLS_SORT_CI使其不区分大小写:

SQL> alter session set nls_sort=latin_ci;

Session altered

SQL> select model from client_source order by model;

MODEL
--------------------
Default
default1
ipad
iphone
iphone simulator
ipod
ipod touch
test
Unknown
1234
4g

您可以使用 移动前面的数字CASE

SQL> SELECT model
  2    FROM client_source
  3   ORDER BY CASE WHEN regexp_like(model, '^[[:digit:]]')
  4                    THEN 0
  5                    ELSE 1
  6            END,
  7            model;

MODEL
--------------------
1234
4g
Default
default1
ipad
iphone
iphone simulator
ipod
ipod touch
test
Unknown

此外,您可以使用该NLSSORT函数来修改单个查询的排序首选项:

select model from client_source order by NLSSORT(model, 'NLS_SORT = LATIN_CI');
于 2013-02-15T16:25:23.820 回答