Question

Silly question but I can't find a reasonable answer.

I need to order a field containing hexadecimal values like :

select str from
    (
    select '2212A' str from dual union all
    select '2212B'     from dual union all
    select '22129'     from dual union all
    select '22127'     from dual union all
    select '22125'     from dual union all
    select '22126'     from dual
   ) t
   order by str asc;

This request give :

STR         
------------
2212A
2212B
22125
22126
22127
22129

I would like

STR         
------------
22125
22126
22127
22129
2212A
2212B

How can I do that ?

Was it helpful?

Solution

Are these HEX numbers? Will the max letter be F? Then convert hex to decimal:

select str
from t
order by to_number(str,'XXXXXXXXXXXX');

EDIT: Stupid me. The title says it's hex numbers :P So this solution should work for you.

OTHER TIPS

You have to clarify further what you want to achieve, but in general, you can sort your table on that column and the first row is the one with smallest value:

SELECT * FROM mytable ORDER BY mycolumn

If you want one record only:

SELECT * FROM mytable ORDER BY mycolumn WHERE rownum = 1

You can try something like a try_convert() in case the conversion fails.

SELECT * FROM table_name ORDER BY TRY_CONVERT(VARBINARY, column_name) ASC;

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top