How to order by case insensitive ASC or DESC, with

2020-07-18 06:26发布

How to order by case insensitive ASC or DESC for P/L sql 11g. this p/l sql basic question but i can't find good answer in Google please tell how to sort the select result case insensitive

this what i tried

   SELECT  DISTINCT
            asssss,
            saas_acc
      FROM DUAL
   UNION SELECT '--ALL--','ALL' FROM DUAL
   ORDER BY  upper(asssss) ASC ;

that gave to me ORA-01785: ORDER BY item must be the number of a SELECT-list expression

4条回答
Fickle 薄情
2楼-- · 2020-07-18 07:01

The simplest option would be to sort by the upper- (or lower-) case column data

ORDER BY UPPER( column_name )
查看更多
相关推荐>>
3楼-- · 2020-07-18 07:12

You can use upper or lower functions.

order by upper(columnName)

Update1

Try removing order-by clause from your query which will give you correct error, which is ORA-00904: "SAAS_ACC": invalid identifier. So you can search on google for this error or ask another question on SO.

Also have a look at how to use order by in union.

查看更多
够拽才男人
4楼-- · 2020-07-18 07:15

To sort case insensitive you need to set the NLS_COMP to ANSI

   NLS_COMP=ANSI

Details: http://www.orafaq.com/node/999

查看更多
狗以群分
5楼-- · 2020-07-18 07:25

DISTINCT actually filtered the UNIQUE content in the result set, with whatever expressions given in the SELECT clause.

We cannot order it using a Different expression or column name. Please see the example here.

SQL> l
  1  SELECT DISTINCT  (col1),(col2)
  2  FROM
  3    ( SELECT 'Hello' col1,'World' col2 FROM DUAL
  4    UNION ALL
  5    SELECT 'HELLO','WORLD' FROM DUAL
  6*   )
SQL> /

COL1  COL2
----- -----
HELLO WORLD
Hello World

You can see that DISTINCT is CASE SENSITIVE here.(2 rows displayed)


So, let me Do a UPPER() on both columns.

SQL> l
  1  SELECT DISTINCT UPPER (col1),UPPER(col2)
  2  FROM
  3    ( SELECT 'Hello' col1,'World' col2 FROM DUAL
  4    UNION ALL
  5    SELECT 'HELLO','WORLD' FROM DUAL
  6*   )
SQL> /

UPPER UPPER
----- -----
HELLO WORLD

Just 1 row is Displayed, ignoring the case.


Coming back to the actual problem. To order something on a DISTINCT Resultset, it has to be a part of DISTINCT clause's expression/column.

So, When you issue DISTINCT COL1,COl2, the order by may be by COL1 or COL2/.. it cannot be COL3 or even UPPER(COL1) because UPPER() makes a different expression conflicting the expression over DISTINCT.


Finally, Answer for your Question would be

if you want your ORDER to be case-insensitive, DISTINCT also has to the same way! As given below

SELECT  DISTINCT
            UPPER(asssss),
            saas_acc
      FROM DUAL
   ORDER BY  upper(asssss) ASC ;

OR if UNION has to be used, better do this, or same as above one.

SELECT * FROM    
(
  SELECT  DISTINCT asssss as asssss,
          saas_acc
  FROM DUAL
 UNION
 SELECT '--ALL--','ALL' FROM DUAL
)
ORDER BY  upper(asssss) ASC ;

Out of my own Experience, I had always felt, what ever expression/column is specified in the ORDER BY, it is implicitly taken to final SELECT as well. Ordering is just based on the column number(position) in the result actually . In this situation, DISTINCT COL1,COl2 is already there. When you give ORDER BY UPPER(COL1), it will be tried to append into the SELECT expression, which is NOT possible at all. So, Semantic check itself, would disqualify this query with an Error!

查看更多
登录 后发表回答