• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Sort Hex value

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've got a varchar2-column that contains a hexadecimal number (0-9, A-F).

Using "order by", Oracle sorts it like this:

A10
BFF
010
900

I would like it to be sorted in the natural order, like this:

010
900
A10
BFF

Any ideas?

Torsten
 
Marshal
Posts: 79700
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

But that is the natural order. If it is a varchar, then it is a type of "text" and "text" is sorted in alphabetical order like that. Suggest you download them with JDBC, parse them to Integer (or Long or BigInteger) objects with a radix of 0x10, put them in an array, List<Number> or similar, then sort them.

Is there a built-in function in SQL which parses text into numbers? I don't remember seeing it. It is worthwhile going through the Oracle handbooks to see whether there is a special PLSQL or Oracle function like that.
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Torsten,
TO_NUMBER can convert a hex string to a number and TO_CHAR can convert a number to hex. See this converter list for an example.

You don't really need to convert it back to hex for the sort though.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TO_NUMBER( your_column, 'XXX' )
 
The City calls upon her steadfast protectors. Now for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic