Oracle ORDER BY 中忽略与号 (&) 字符

发布于 2024-11-01 20:25:20 字数 844 浏览 8 评论 0原文

我正在 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 子句不考虑该情况。有谁知道这是否是预期行为或者我是否缺少某些配置步骤?我知道需要转义&符号以进行插入或更新。但问题是该角色已经在表中了!

预先感谢各位。

I am running a query in Oracle and that query is ordered by a column that may have values with ampersand. However, it seems that ampersand is being ignored by the sorting algorithm.

For instance:

select * from (
    select '&' txt from dual
    union
    select 'P' txt from dual
    union
    select 'N' txt from dual
)
order by txt

Prints exactly (and correctly, I guess):

&
N
P

However, if I change the text with '&' to '&Z', the result changes:

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

Result is then:

N
P
&Z

If I change 'Z' to 'A' then the result is:

&A
N
P

It seems that the '&' is not being considered by ORDER BY clause. Does anyone know if this is expected behavior or if I am missing some configuration step? I understand that one needs to escape ampersand for inserts or updates. But thing is that the character is already in the table!

Thanks in advance, guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-11-08 20:25:20

这是语言排序的效果:

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 值进行排序。

It is the effect of linguistic sorting:

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

It does make a difference for what language you are sorting. Some characters have different values depending on their language. With binary you sort using the ascii values.

青春如此纠结 2024-11-08 20:25:20

这可能取决于您的 NLS_LANGUAGE 和 NLS_SORT 设置。

我在 Oracle 10g 上得到以下信息,其中 NLS_LANGUAGE = AMERICAN 和 NLS_SORT = (null)。

TXT 
--- 
&   
N   
P   

TXT 
--- 
&Z  
N   
P   

It may depend on your NLS_LANGUAGE and NLS_SORT setting.

I get the following on Oracle 10g, with NLS_LANGUAGE = AMERICAN and NLS_SORT = (null).

TXT 
--- 
&   
N   
P   

TXT 
--- 
&Z  
N   
P   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文