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:
Set
NLS_COMP='ANSI'
and'NLS_SORT=BINARY_CI'
,CI
suffix means case-insensitive, session or system wide by usingalter session
oralter 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 colResult:
COL
---
A
a
b
BChange case of the character literal by using either
upper()
orlower()
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
Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
---|---|---|---|---|---|---|
utf8mb4_0900_ai_ci | utf8mb4 | 255 | Yes | Yes | 0 | NO PAD |
utf8mb4_0900_as_ci | utf8mb4 | 305 | Yes | 0 | NO PAD | |
utf8mb4_0900_as_cs | utf8mb4 | 278 | Yes | 0 | NO PAD | |
utf8mb4_0900_bin | utf8mb4 | 309 | Yes | 1 | NO PAD | |
utf8mb4_bin | utf8mb4 | 46 | Yes | 1 | PAD SPACE | |
utf8mb4_croatian_ci | utf8mb4 | 245 | Yes | 8 | PAD SPACE | |
utf8mb4_cs_0900_ai_ci | utf8mb4 | 266 | Yes | 0 | NO PAD | |
utf8mb4_cs_0900_as_cs | utf8mb4 | 289 | Yes | 0 | NO PAD | |
utf8mb4_czech_ci | utf8mb4 | 234 | Yes | 8 | PAD SPACE | |
utf8mb4_danish_ci | utf8mb4 | 235 | Yes | 8 | PAD SPACE |
Related Topics
Show a One to Many Relationship as 2 Columns - 1 Unique Row (Id & Comma Separated List)
What Is the Order of Execution for This SQL Statement
Postgresql Multi Insert...Returning with Multiple Columns
Excel Function to Make SQL-Like Queries on Worksheet Data
SQL Server Ignore Case in a Where Expression
Mysql: What Is a Reverse Version of Like
Saving Changes After Table Edit in SQL Server Management Studio
How to Compare SQLite Timestamp Values
Conditional Sum in Group by Query Mssql
Write a Number with Two Decimal Places SQL Server
How to Import Text Files with the Same Name and Schema But Different Directories into Database
Is the Like Operator Case-Sensitive with SQL Server
SQL Server Convert Integer to Binary String
How to Return Multiple Rows from the Stored Procedure? (Oracle Pl/Sql)