• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

Oracle SQL character data need sorted numerically

I have a column in my database that has data that begins with numbers but can have letters, that is why it is a varchar field.  For example, 1, 1a, 2, 2a, 3, 3a, 4,5,6, 7, 8, 9 10
I need to sort it in ascending order, but it sorts like the following:
1
10
2
20
3
30

How can I get it to do that.????
Thank you.
0
newtoperlpgm
Asked:
newtoperlpgm
  • 4
  • 2
1 Solution
 
sdstuberCommented:
select * from yourtable order by to_number(regexp_replace(yourcharacterfield,'[^0-9]'));
0
 
sdstuberCommented:
another variation

select * from yourtable order by to_number(regexp_substr(yourcharacterfield,'^[0-9]+'));
0
 
newtoperlpgmAuthor Commented:
is there way to select it as a number rather than having to order by?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
newtoperlpgmAuthor Commented:
I tried the following but it doesn't work.
select regexp_subst(SAMPLE_NO,'^[0-9]+') from Table
Thanks.
0
 
sdstuberCommented:
put either of the conditions I used in the order by

select to_number(regexp_substr(yourcharacterfield,'^[0-9]+')) from yourtable

will return '123abc'  as 123



when you say "doesn't work"  - what doesn't work about it?

wrong results, error, no results, keyboard catches fire?
0
 
sdstuberCommented:
simple test cases of either method

select to_number(regexp_substr(sample_no,'^[0-9]+')) from
(select '123abc' sample_no from dual);



select to_number(regexp_replace(sample_no,'[^0-9]')) from
(select '123abc' sample_no from dual)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now