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
The simplest option would be to sort by the upper- (or lower-) case column data
You can use
upper
orlower
functions.Update1
Try removing
order-by
clause from your query which will give you correct error, which isORA-00904: "SAAS_ACC": invalid identifier
. So you can search ongoogle
for this error or ask another question on SO.Also have a look at how to use order by in union.
To sort case insensitive you need to set the NLS_COMP to ANSI
Details: http://www.orafaq.com/node/999
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.
You can see that
DISTINCT
isCASE SENSITIVE
here.(2 rows displayed)So, let me Do a
UPPER()
on both columns.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 ofDISTINCT
clause's expression/column.So, When you issue
DISTINCT COL1,COl2
, the order by may be byCOL1
orCOL2
/.. it cannot beCOL3
or evenUPPER(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 belowOR if UNION has to be used, better do this, or same as above one.
Out of my own Experience, I had always felt, what ever expression/column is specified in the
ORDER BY
, it is implicitly taken to finalSELECT
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 giveORDER BY UPPER(COL1)
, it will be tried to append into theSELECT
expression, which isNOT
possible at all. So, Semantic check itself, would disqualify this query with an Error!