我在 Oracle 中运行一个查询,该查询按列排序,该列的值可能带有 & 符号。但是,排序算法似乎忽略了 & 符号。
例如:
select * from (
select '&' txt from dual
union
select 'P' txt from dual
union
select 'N' txt from dual
)
order by txt
准确打印(我猜是正确的):
&
N
P
但是,如果我将带有“&”的文本更改为“&Z”,结果会发生变化:
select * from (
select '&'||'Z' txt from dual // concatenating just to
// avoid variable substitution
union
select 'P' txt from dual
union
select 'N' txt from dual
)
order by txt
结果是:
N
P
&Z
如果我将“Z”更改为“A”,则结果为:
&A
N
P
似乎 ORDER BY 子句未考虑“&”。有谁知道这是否是预期的行为,或者我是否缺少某些配置步骤?我知道需要转义符号以进行插入或更新。但问题是角色已经在表中了!
提前致谢,伙计们。
最佳答案
是语言排序的效果:
SQL> alter session set nls_sort=binary;
Session altered.
SQL> get afiedt.buf
1 select * from (
2 select '&' txt from dual
3 union
4 select '&'||'Z' txt from dual
5 union
6 select '&'||'A' txt from dual
7 union
8 select 'P' txt from dual
9 union
10 select 'N' txt from dual
11 )
12* order by txt
SQL> /
TX
--
&
&A
&Z
N
P
SQL> alter session set nls_sort = 'Dutch';
Session altered.
SQL> get afiedt.buf
1 select * from (
2 select '&' txt from dual
3 union
4 select '&'||'Z' txt from dual
5 union
6 select '&'||'A' txt from dual
7 union
8 select 'P' txt from dual
9 union
10 select 'N' txt from dual
11 )
12* order by txt
SQL> /
TX
--
&
&A
N
P
&Z
它确实会影响您排序的语言。某些字符根据其语言具有不同的值。对于二进制,您可以使用 ascii 值进行排序。
关于oracle - Oracle ORDER BY 中将忽略与号 (&) 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5682949/