2015-06-15 93 views
5

我想按以下順序排列以下文本,但在嘗試下列查詢後,它無法正常工作。在oracle中按不區分大小寫的順序排列

要訂購的值是「A」,「B」,「Y」,「Z」,「a」,「b」,「y」,「z」。

預期的結果「ZzYyBbAa」

SELECT COL FROM TABLE ORDER BY COL DESC; 
SELECT COL FROM TABLE ORDER BY UPPER/LOWER(COL) DESC; Result-> ZzYybBaA 
SELECT COL FROM TABLE ORDER BY NLS_UPPER/NLS_LOWER(COL) DESC; Result-> ZzYybBaA 

回答

8

首先,你可以通過上(或下)列的情況下訂購,但一旦你做到了這一點,然後需要由排序文本本身以獲得對首字母的順序;例如:

with sample_data as (select 'A' txt from dual union all 
        select 'B' txt from dual union all 
        select 'Y' txt from dual union all 
        select 'Z' txt from dual union all 
        select 'a' txt from dual union all 
        select 'b' txt from dual union all 
        select 'y' txt from dual union all 
        select 'z' txt from dual) 
select txt 
from sample_data 
order by upper(txt) desc, txt; 


TXT 
--- 
Z 
z 
Y 
y 
B 
b 
A 
a 
相關問題