4

I have a simple table

 CREATE TABLE TRIAL 
   (    "COL" VARCHAR2(20 BYTE)
   )

and I insert there two values, '0', and 'A'.

The query

select * from trial order by col

returns

A
0

in this order, while the query

select * from trial where col>'A'

returns no results. What could be the reason for such behaviour, and is there some simple trick, without changing db configuration, to get order by and > behave in a consistent manner?

EDIT:

to answer the comments:

select * from v$parameter where name like 'nls_sort'

returns

and

select dump(col,16),col from trial

returns

Typ=1 Len=1: 30 0
Typ=1 Len=1: 41 A
11
  • 3
    I tried the query and select * from trial order by col for me returns 0,A . Are you sure you are not missing something? Try select dump(col,16),col from trial to see the ASCII code of each character. Jul 8, 2015 at 14:12
  • Check your nls_sort parameter. select * from v$parameter where name like 'nls_sort';; Jul 8, 2015 at 14:15
  • Not able to reproduce your first result set with provided sample values. I can reproduce your second, as that is working correctly as the answer by Andrew demonstrated. Jul 8, 2015 at 14:36
  • @StefanYordanov I just edited my question to answer yours
    – John Donn
    Jul 8, 2015 at 14:41
  • 1
    OK, so what are NLS_SORT and NLS_COMP set to is you query NLS_SESSION_PARAMETERS? I can duplicate what you see with binary/edbdic for example, but it would be interesting to see what you're actually using. (Read more about why these matter here)
    – Alex Poole
    Jul 8, 2015 at 15:03

2 Answers 2

3

It should be sorting by the binary/ASCII value of the string.

http://www.ascii-code.com/

Translating the values

  • 0 => 48
  • A => 65

When you sort by col, the default is ascending, so I would expect the 0 to come first, then the A.

When you ask for > 'A', you are asking for > 65, and neither 'A' or '0' is greater, so that makes sense.

As mentioned in the comments, I would check your sort NLS_SORT value to see if something is odd there for the sorting:

https://docs.oracle.com/cd/B19306_01/server.102/b14237/initparams130.htm#REFRN10127

You can also make sure this matches your NLS_COMP value:

https://docs.oracle.com/cd/B19306_01/server.102/b14237/initparams120.htm#REFRN10117

You can find more info in this answer:

https://stackoverflow.com/a/7191170/137649

1

NOTE : The actual issue turned out to be with NLS_SORT parameter. Please have a look at Oracle – Case Insensitive Sorts & Compares/ to get a good hold and understanding on the specific parameter value.

Actual Issue

The problem is actually due to NLS_SORT parameter value modified to 'WEST_EUROPEAN' from 'BINARY'.

Setup

SQL> CREATE TABLE TRIAL
  2     (    "COL" VARCHAR2(20 BYTE)
  3     );

Table created.

SQL> INSERT INTO trial(col) VALUES('0');

1 row created.

SQL> INSERT INTO trial(col) VALUES('A');

1 row created.

SQL> COMMIT;

Commit complete.

SQL> SELECT * FROM trial ORDER BY col;

COL
--------------------
0
A

NLS Parameter values

SQL> SHOW PARAMETER NLS_SORT;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------
nls_sort                             string      BINARY

SQL> SHOW PARAMETER NLS_COMP;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------
nls_comp                             string      BINARY

Let's change the NLS_SORT parameter value:

SQL> ALTER SESSION SET NLS_SORT='WEST_EUROPEAN';

Session altered.

Error reproduce

SQL> SELECT * FROM trial ORDER BY col;

COL
--------------------
A
0

So, now the sorting of the values has changed with the change in the value of the NLS_SORT parameter.

8
  • 1
    I don't think you reproduced OP's result with ORDER BY col.
    – shawnt00
    Jul 8, 2015 at 14:48
  • but then this does not explain the sort behaviour as I see it, does it?
    – John Donn
    Jul 8, 2015 at 14:48
  • @shawnt00 I don't think OP is stating complete details. Jul 8, 2015 at 14:51
  • @LaitKumar I suspect that what I see is connected to the fact that the sort is "linguistic" instead of "binary" in my configuration. However your answer suggested to me a possible way out: select * from trial order by ascii(col). This is consistent with >. Thank you, I upvoted your answer for that.
    – John Donn
    Jul 8, 2015 at 14:58
  • 1
    @AlexPoole I just figured it out. So many edits, so many comments, I just got confused. I have updated the answer, and removed my previous comments. Coffee didn't help today :-) Jul 8, 2015 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.