Solved

Order by last 2 numbers in varchar colum Oracle

Posted on 2014-02-12
3
383 Views
Last Modified: 2014-02-12
I created a query that returns a list of tools by tool station number.  I need to order the results a few different ways:

If tool_station_num is 3 or 4 characters I need the last 2 characters
If tool_station_num is 2 characters I need the last 1 character

Below is are 2 examples of the the tools that I get from my query and the output I am looking for.

Query outputs tools like this
T134
T141
T201
T237
T333
T340
T342
T351

need to sort like this
T201
T333
T134
T237
T340
T141
T342
T351

Another example from query
T10
T11
T2
T31
T33
T40
T42
T48
T55
T66
T68
T69
T7
T91
T92
T93

desired output
T2
T7
T10
T11
T31
T33
T40
T42
T48
T55
T66
T68
T69
T91
T92
T93
0
Comment
Question by:dustock
  • 2
3 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 39854670
select  * from yourtable
order by to_number(regexp_substr( tool_station_num,'[0-9]{1,2}$'))
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39854680
or, if you have a really old version that doesn't support regular expressions try this...

  SELECT *
    FROM yourtable
ORDER BY TO_NUMBER(
             TRANSLATE(
                 SUBSTR(tool_station_num, -2),
                 '0123456789' || SUBSTR(tool_station_num, -2),
                 '0123456789'
             )
         );
0
 
LVL 9

Author Comment

by:dustock
ID: 39854707
This database is Oracle 9i, I was having problems with the first one, but the second one seems to be working fine.  Thanks for the fast response!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now