In an Oracle SQL query which uses binary sorting rules, numbers are sorted before letters. But if the query uses a language specific collation to enable linguistic sorting, then numbers are sorted after letters.
Example: https://dbfiddle.uk/BBfvb2lh
This differs from e.g. .NET sorting, where even with linguistic sorting the numbers are sorted before letters.
Example: https://dotnetfiddle.net/65G3na
How can I use linguistic sorting and sort numbers before letters in an Oracle SQL query?
Assuming you can accept sorting numbers first, followed by everything else second, then you could use the following logic:
SELECT val
FROM yourTable
ORDER BY
CASE WHEN REGEXP_LIKE(val, '^[0-9]') THEN 1 ELSE 2 END, -- numbers first
val; -- then sort by val
5
I’ve got a simpler solution in my follow up question:
The order is correct if I replace COLLATE XGERMAN_AI
with COLLATE GENERIC_M_AI
.
Extended examples:
Oracle: https://dbfiddle.uk/TOzViGhK
.NET: https://dotnetfiddle.net/gytC9a
Try this bro:
create table t(val varchar2(100));
insert into t
select 'AD4' from dual union all
select 'aA1' from dual union all
select 'äB2' from dual union all
select 'aC3' from dual union all
select '1' from dual union all
select '2' from dual union all
select 'XAA2' from dual union all
select 'X1A1' from dual;
commit;
SELECT val
FROM t
ORDER BY substr(REGEXP_REPLACE(val, '[[:alpha:]]'),0)
This answer is almost the same the link you post (How to sort numbers before letters with linguistic sorting?).
Using ‘[[:alpha:]]’) in this case wiil consider number first inside
Fernando Neto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1