• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3371
  • Last Modified:

oracle underscore sort problem

I have a problem when I execute an SQL query on oracle:
Ex:
select uid, title from table_name order by title

The title field is of type string but the sorting ignores the underscore character'_' when is sorting records. Ex:

aaa
bbb
__cccc
cddd

I want that underscore to be considered when sorting. How can I sove this problem?
0
lehene
Asked:
lehene
1 Solution
 
mona1974Commented:
If "title" field is varchar2 type the sorting will not ingnore underscore character. Could you tell me exactly the type of "title" field?
0
 
leheneAuthor Commented:
title field is of type varchar2, size 255
0
 
jtriftsMI and AutomationCommented:
The underscores ARE taken into account when sorting using ORACLE default sort.

The sequence of sorting is similar to that of the ASCII table.  In the case of underscores, it represents character 95 in the ascii table.

Thus the underscores will come AFTER uppercase characters, but BEFORE lowercase characters.

I tested with the following results:

AAA
BBB
CCC
DDD
_CC
__C
__C
__c
_cc
aaa
bbb
ccc
ddd

IF you have a particular placement in mind, then you can try using a DECODE function or a REPLACE function in your ORDER BY clause.

JT
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leheneAuthor Commented:
title field is of type varchar2, size 255
0
 
leheneAuthor Commented:
In my case underscore is just not considered does not matter if the characters are lowercase or uppercase.
0
 
jtriftsMI and AutomationCommented:
I'm afraid I can't believe that.  Oracle does not ignore underscores.  Do you only use upper or lower case?

Can you show us the results from a sort that has both upper and lower case where the results are sorted by the the field that starts with the underscore and demonstrate that it has been ignored? (i.e. please show the cursor/select code and the output results)

JT
0
 
KirilloffCommented:
There is NLS_SORT parameter that affect sort order. If you use ASCII7 or BINARY then underscores are taken into account. If you use other sortings then underscores are insignificant. Try to use

ALTER SESSION SET NLS_SORT=ASCII7

But in this case you will sort strings by their ascii codes: at first some signs, then digits, capital letters, underscore and some signs, then small letter.
0
 
WadhwaCommented:
Hi Lethene,

The above comments are good in all respect. I want you to check the following.
connect with sys and execute the following query

ora816 SamSQL :> select * from nls_database_parameters
  2  where parameter='NLS_SORT';


it will tell you what the value of sort (ASCII/BINARY/WEST_EUROPEAN etc )

If your database is on NT check the registry entry on
HKLM/SOFTWARE/ORACLE or for multiple home HKLM/SOFTWARE/ORACLE/HOMEn

NLS_SORT on the client machine also effect the results of sorting.


Here are the simple test for ascii7/binary/west_european sort. Your above result matches with the west_european sort

ora816 SamSQL :> alter session set nls_sort=ascii7
  2  /

Session altered.

Elapsed: 00:00:00.00
ora816 SamSQL :> select x,ascii(x) from testsort order by 1;

X  ASCII(X)
----------
__cc        95
_ccc        95
aaa        97
bbbb        98
ccc        99
dddd       100

6 rows selected.

Elapsed: 00:00:00.40
ora816 SamSQL :> alter session set nls_sort=BINARY
  2  /

Session altered.

Elapsed: 00:00:00.00
ora816 SamSQL :> select x,ascii(x) from testsort order by 1;

X  ASCII(X)
----------
__cc        95
_ccc        95
aaa        97
bbbb        98
ccc        99
dddd       100

6 rows selected.

Elapsed: 00:00:00.40
ora816 SamSQL :> alter session set nls_sort=west_european;

Session altered.

Elapsed: 00:00:00.00
ora816 SamSQL :> select x,ascii(x) from testsort order by 1;

X    ASCII(X)
---
aaa        97
bbbb       98
__cc       95
_ccc       95
ccc        99
dddd       100


6 rows selected.

Elapsed: 00:00:00.50
ora816 SamSQL :>

Hope the above will explain why you are not seeing the desired sorting results.

Goodluck
Sam
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now