How to Use SQL Order by Statement to Sort Results Case Insensitive

How to use SQL Order By statement to sort results case insensitive?

You can also do ORDER BY TITLE COLLATE NOCASE.

Edit: If you need to specify ASC or DESC, add this after NOCASE like

ORDER BY TITLE COLLATE NOCASE ASC

or

ORDER BY TITLE COLLATE NOCASE DESC

how to sort by case insensitive alphabetical order using COLLATE NOCASE

Oracle does not support COLLATE NOCASE option of the order by clause. To be able to perform case-insensitive ordering you have two options:

  1. Set NLS_COMP='ANSI' and 'NLS_SORT=BINARY_CI', CI suffix means case-insensitive, session or system wide by using alter session or alter system statement:

    alter session set nls_comp='ANSI';
    alter session set nls_sort='BINARY_CI';
    with t1(col) as(
    select 'A' from dual union all
    select 'a' from dual union all
    select 'b' from dual union all
    select 'B' from dual
    )
    select *
    from t1
    order by col

    Result:

    COL
    ---
    A
    a
    b
    B
  2. Change case of the character literal by using either upper() or lower() function.

      with t1(col) as(
    select 'A' from dual union all
    select 'a' from dual union all
    select 'b' from dual union all
    select 'B' from dual
    )
    select *
    from t1
    order by upper(col)

    result:

    COL
    ---
    A
    a
    b
    B

Edit

but i need the UpperCase to preceed any LowerCase eg. Alan, alan, Brian, brian, Cris

This is not the case-insensitive ordering, rather quite contrary in some sense. As one of the options you could do the following to produce desired result:

with t1(col) as(
select 'alan' from dual union all
select 'Alan' from dual union all
select 'brian' from dual union all
select 'Brian' from dual union all
select 'Cris' from dual
)
select col
from ( select col
, case
when row_number() over(partition by lower(col)
order by col) = 1
then 1
else 0
end as rn_grp
from t1
)
order by sum(rn_grp) over(order by lower(col))

Result:

COL
-----
Alan
alan
Brian
brian
Cris

How to sort the results in case sensitive manner in PostgreSQL?

You could be converting as BYTEA

select  your_column 
from your_table
order by your_column::TEXT::BYTEA

Order by case insensitive in oracle

First of all, you can order by the UPPER (or LOWER) case of the column, but once you've done that, you then need to sort by the text itself to get the order on the initial letter; eg:

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

Sql ORDER BY alphabetically no casesensitive

The rules for comparing text values is the collation; there are many many collations available in SQL Server, and most have both case-sensitive and case-insensitive options.

If you don't want to change the collation (in particular, if this applies only to specific cases), you can also use functions like LOWER / UPPER, but this cannot make efficient use of indexes. A hybrid approach is to store redundant information: store the original data in one column, and the standardized data (perhaps all lower-case-invariant) in a second column. Then you can index the two separately (as you need), and operate on either the original or standardized data. You would normally only display the original data, though. Persisted+calculated+indexed columns might work well here, as then it is impossible to get inconsistent data (the server is in charge of the calculated column).

CursorLoader - order by in SQL is case insensitive

I'm using this question/answers as source:

While ordering, you can "convert" the data to lower/upper case. Data retrieved won't be affected since you are changing the ORDER BY statement.

return new CursorLoader(this, CONTENT_URI, projection, null, null, "UPPER(COLUMN_NAME)");

SQL ORDER BY a string value: What is it comparing? (Case sensitive?)

In MySQL, it depends on the effective collation. Collation is the set of rules that determine the position of characters in an ordered set and what characters are considered equal, and typically involve natural language rules. For example, Spanish used to have ch as an independent letter located betwen c and d and then switched to being just individual c and h; MySQL has collations for both.

You can see available collations with these commands:

SHOW COLLATION; -- Display all
SHOW COLLATION WHERE charset = 'utf8mb4'; -- Filter by encoding


Leave a reply



Submit