Solved

Order by last 2 numbers in varchar colum Oracle

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Database creation fails 5 50
alter database link to change the password 2 52
SQL Query 34 82
Oracle 12c database link between pdb not working 20 48
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to take different types of Oracle backups using RMAN.

863 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

27 Experts available now in Live!

Get 1:1 Help Now