ORACLE在字符串字段

时间:2018-01-19 11:53:26

标签: sql oracle sql-order-by

我无法确定如何在此查询中设置此order by。 我想首先按数字排序,然后按char排序。也许将每个角色改为HEX会有效吗?我不知道。我按照

订购时会把结果输出来
COLA   COLB
----   ----
00     1
000A   2
000B   2
000C   2
000D   2
000F   2
000G   2
000H   2
000I   2
0000   2
0001   2
0002   2
0003   2
00030A 3
00030B 3
000300 3

我想得到的结果是:

COLA   COLB
----   ----
00     1
0000   2
0001   2
0002   2
0003   2
000A   2
000B   2
000C   2
000D   2
000F   2
000G   2
000H   2
000I   2
000300 3
00030A 3
00030B 3

这是我的疑问:

with table1
as (
  select '00' COLA, 1 COLB from dual union
  select '000A' COLA, 2 COLB from dual union
  select '000B' COLA, 2 COLB from dual union
  select '000C' COLA, 2 COLB from dual union
  select '000D' COLA, 2 COLB from dual union
  select '000E' COLA, 2 COLB from dual union
  select '000F' COLA, 2 COLB from dual union
  select '000G' COLA, 2 COLB from dual union
  select '000H' COLA, 2 COLB from dual union
  select '000I' COLA, 2 COLB from dual union
  select '0000' COLA, 2 COLB from dual union
  select '0001' COLA, 2 COLB from dual union
  select '0002' COLA, 2 COLB from dual union
  select '0003' COLA, 2 COLB from dual union
  select '00030A' COLA, 3 COLB from dual union
  select '00030B' COLA, 3 COLB from dual union
  select '000300' COLA, 3 COLB from dual 
)
select * from table1 order by COLB, COLA

我已经尝试过使用LPAD,但它没有用。

4 个答案:

答案 0 :(得分:3)

当我在我的数据库中运行您的示例时,我得到了正确的'按照您的列表排序顺序。检查NLS_SORT设置;我的目标是BINARY。

通过更改会话来尝试;

ALTER SESSION SET nls_sort='BINARY';

此参数有很多可能性,因此如果您想进行实验,可以在V $ NLS_VALID_VALUES视图中找到可能性。

我也发现你可以做以下事情;

SELECT * FROM table1 ORDER BY colb, NLSSORT(cola, 'NLS_SORT=BINARY')

答案 1 :(得分:2)

一种方法是提取数字:

order by colb,
         cast(regexp_substr(cola, '[0-9]', 1, 1) as number),
         cola

答案 2 :(得分:0)

请试试这个

with table1
as (
 select '00' COLA, 1 COLB from dual union
select '000A' COLA, 2 COLB from dual union
select '000B' COLA, 2 COLB from dual union
select '000C' COLA, 2 COLB from dual union
select '000D' COLA, 2 COLB from dual union
select '000E' COLA, 2 COLB from dual union
select '000F' COLA, 2 COLB from dual union
select '000G' COLA, 2 COLB from dual union
select '000H' COLA, 2 COLB from dual union
select '000I' COLA, 2 COLB from dual union
select '0000' COLA, 2 COLB from dual union
select '0001' COLA, 2 COLB from dual union
select '0002' COLA, 2 COLB from dual union
select '0003' COLA, 2 COLB from dual union
select '00030A' COLA, 3 COLB from dual union
select '00030B' COLA, 3 COLB from dual union
select '000300' COLA, 3 COLB from dual 
)
select * from table1 order by COLB, to_number(regexp_substr(COLA, '^[[:digit:]]*')), COLA

答案 3 :(得分:0)

您可以使用TRANSLATE功能:

  with table1 as (
  select '00' COLA, 1 COLB from dual union
  select '000A' COLA, 2 COLB from dual union
  select '000B' COLA, 2 COLB from dual union
  select '000C' COLA, 2 COLB from dual union
  select '000D' COLA, 2 COLB from dual union
  select '000E' COLA, 2 COLB from dual union
  select '000F' COLA, 2 COLB from dual union
  select '000G' COLA, 2 COLB from dual union
  select '000H' COLA, 2 COLB from dual union
  select '000I' COLA, 2 COLB from dual union
  select '0000' COLA, 2 COLB from dual union
  select '0001' COLA, 2 COLB from dual union
  select '0002' COLA, 2 COLB from dual union
  select '0003' COLA, 2 COLB from dual union
  select '00030A' COLA, 3 COLB from dual union
  select '00030B' COLA, 3 COLB from dual union
  select '000300' COLA, 3 COLB from dual 
)
select * from table1 order by COLB, TRANSLATE(COLA,'0123456789','0123456789'),TRANSLATE(COLA,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','ABCDEFGHIJKLMNOPQRSTUVWXYZ')