Why does Oracle's varchar sort order not match the behavior of varchar comparison?

后端 未结 1 489
轮回少年
轮回少年 2020-12-09 00:07

An SQL statement like:

select * from (
  select \'000000000000\' as x from dual
  union
  select \'978123456789\' as x from dual
  union 
  select \'B002AACD         


        
相关标签:
1条回答
  • 2020-12-09 00:16

    Peter,

    the behaviour of the sorting is regulated by the NLS_SORT session parameter, whereas the behaviour for comparisons is dependent upon the NLS_COMP parameter. You must have a mismatch.

    I obtain the same result as you do with the following parameters:

    SQL> SELECT *
      2    FROM nls_session_parameters
      3   WHERE parameter IN ('NLS_COMP', 'NLS_SORT');
    
    PARAMETER                      VALUE
    ------------------------------ ----------------------------------------
    NLS_SORT                       FRENCH
    NLS_COMP                       BINARY
    

    However when the two are matched the result is consistent:

    SQL> alter session set nls_comp=LINGUISTIC;
    
    Session altered
    
    SQL> select * from (
      2    select '000000000000' as x from dual
      3    union
      4    select '978123456789' as x from dual
      5    union
      6    select 'B002AACD0A' as x from dual
      7  ) /*where x>'000000000000'*/ order by x;
    
    X
    ------------
    B002AACD0A
    000000000000
    978123456789
    
    SQL> select * from (
      2    select '000000000000' as x from dual
      3    union
      4    select '978123456789' as x from dual
      5    union
      6    select 'B002AACD0A' as x from dual
      7  ) where x > '000000000000' order by x;
    
    X
    ------------
    978123456789
    
    0 讨论(0)
提交回复
热议问题