Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort by a table column in varying cases (Oracle)

How can I sort a table with a column of varchar2 with characters in varying cases (UPPER and lower)?

For example, when I do an order by of the Name column, I get the following results:

ANNIE
BOB
Daniel
annie
bob

What I want is something like this:

ANNIE
annie
BOB
bob
Daniel
like image 651
acidRain Avatar asked Mar 20 '12 14:03

acidRain


People also ask

What is sort aggregate in Oracle?

SORT AGGREGATE. Retrieval of a single row that is the result of applying a group function to a group of selected rows. Sort :A result set must be sorted, the operation is sort. If this sort is used to return a single row (for example max or min) the options is AGGREGATE.

Can we use case in ORDER BY clause Oracle?

Introduction to Oracle CASE expressionYou can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .

Does ORDER BY column need to be in SELECT?

But, it is possible to order result set by column that is not listed in select list.


2 Answers

Use lower(field), e.g.

select * from tbl order by lower(name)

If you need to address special characters for non-english languages then the other answers about NLSSORT may be what you need. If you don't I would try and KISS and use lower() as it is very easy to remember and use and be read by others (maintainability).

like image 182
Michael Durrant Avatar answered Oct 10 '22 11:10

Michael Durrant


Another option is the use of the NLSSORT function to perform linguistic sorting:

SQL> with test as (select 'ANNIE' as col from dual
  2      union all select 'BOB' from dual
  3      union all select 'Daniel' from dual
  4      union all select 'annie' from dual
  5      union all select 'bob' from dual
  6      union all select 'Ångström' from dual
  7      union all select 'ångström' from dual)
  8  select col
  9  from test
 10  order by nlssort(col, 'NLS_SORT = WEST_EUROPEAN')
 11  /

COL
----------
Ångström
ångström
ANNIE
annie
BOB
bob
Daniel

The advantages are more flexibility. One can sort characters with accents as well as different cases together. One can choose to treat some characters in a language specific way by specifying different values for NLS_SORT. Defines an order within the set of equivalent characters. So 'A' and 'a' are sorted together, but within the 'a's, the upper case comes first. Disadvantages I expect that NLSSORT uses more CPU than LOWER, though I have not bench marked it. And NLSSORT will only use a prefix of longer strings:

The string returned, also known as the collation key, is of RAW data type. The length of the collation key resulting from a given char value for a given collation may exceed 2000 bytes, which is the maximum length of the RAW value returned by NLSSORT. In this case, NLSSORT calculates the collation key for a maximum prefix, or initial substring, of char so that the calculated result does not exceed 2000 bytes. For monolingual collations, for example FRENCH, the prefix length is typically 1000 characters. For multilingual collations, for example GENERIC_M, the prefix is typically 500 characters. The exact length may be lower or higher depending on the collation and the characters contained in char.

like image 13
Shannon Severance Avatar answered Oct 10 '22 12:10

Shannon Severance