I want to sort the search results by alphanumeric column such that it shows rows with alphabets first and the at last it should show rows with numeric characters.
Current result
12
13
14
aa
ab
bb
bd
Expected Result
aa
ab
bb
bd
11
12
13
I want to sort the search results by alphanumeric column such that it shows rows with alphabets first and the at last it should show rows with numeric characters.
Current result
12
13
14
aa
ab
bb
bd
Expected Result
aa
ab
bb
bd
11
12
13
SELECT ... FROM table order by CASE WHEN column < 'A' THEN lpad(column, size, '0') ELSE column END;
Hope this will work.
You want to apply a certain sort order on characters with
You can achieve this by repacing characters in your string, e.g. 'a' -> '01', b -> '02', '0' -> '90', '1' -> '91', 'abc123' -> '010203919293'. You would write a function for this, you would then call from your query.
select col
from mytable
order by get_sortkey(col);
A simple alternative may be to use the TRANSLATE function. The disadvantage: it replaces each character only with another single character, not with two or more as in above example. Knowing the ASCII codes we can use:
select col
from mytable
order by translate(col,
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'0123456789ABCDEFGHIJKLMNOP0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
) collate "C";