Solved

oracle underscore sort problem

Posted on 2001-09-11
8
2,708 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
 

Author Comment

by:lehene
ID: 6473933
title field is of type varchar2, size 255
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 150 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now