Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

oracle underscore sort problem

Posted on 2001-09-11
8
Medium Priority
?
3,109 Views
Last Modified: 2007-12-19
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
Comment
Question by:lehene
8 Comments
 
LVL 1

Expert Comment

by:mona1974
ID: 6473908
If "title" field is varchar2 type the sorting will not ingnore underscore character. Could you tell me exactly the type of "title" field?
0
 

Author Comment

by:lehene
ID: 6473925
title field is of type varchar2, size 255
0
 
LVL 4

Expert Comment

by:jtrifts
ID: 6473931
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:lehene
ID: 6473933
title field is of type varchar2, size 255
0
 

Author Comment

by:lehene
ID: 6473961
In my case underscore is just not considered does not matter if the characters are lowercase or uppercase.
0
 
LVL 4

Expert Comment

by:jtrifts
ID: 6473972
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
 

Expert Comment

by:Kirilloff
ID: 6474172
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
 
LVL 3

Accepted Solution

by:
Wadhwa earned 450 total points
ID: 6474967
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question