Eloijza Pavlova Eloijza Pavlova - 6 months ago 10
SQL Question

Orace SQL - sorting with all upper case first

What if I wanted sorting to have all the upper case first then the lower case?

A
B
C
D
a
b
c
d


I tried searching the net and all I could find was sorting that would make it

a
A
b
B
c
C


etc..

but I wanted all upper case values sorted first then lower case ones.

Any idea? thanks

vkp vkp
Answer

Use a case expression to conditionally order the column, based on upper or lower case. Then order by the original column.

select * from tablename
order by case when upper(col) = col then 1 else 2 end, col