Solved

Order by last 2 numbers in varchar colum Oracle

Posted on 2014-02-12
3
388 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

770 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