Problem solve Get help with specific problems with your technologies, process and projects.

Sorting data in varchar2 field

I have a database field defined as varchar2. Some of the records are numeric and some are alphanumeric. How do I sort out the data?

I have a database field called "checkOrCardNo" which is defined as varchar2. Some records for this field are only numeric and some of them are alphanumeric. How will I be able to sort out the data? When I use an "order by" clause then I have to sort the string values. I have three values: 54316, 5153, 7134. My display is in the order 5153, 54316, 7134. But I want the result set to be displayed as 5153, 7134, 54316. Can anybody help me regarding this?
When I have run into this situation, I have resorted to a simple trick. Since the data is stored in a character datatype, Oracle uses character-based sorting, not numeric sorting. Which is why 54316 appears before 7134. So my trick is to first sort by the length of the value and then by the value itself:

ORDER BY LENGTH(checkorcardno),checkorcardno

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.