Link to home
Start Free TrialLog in
Avatar of dustock
dustockFlag for United States of America

asked on

Order by last 2 numbers in varchar colum Oracle

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
Avatar of Sean Stuber
Sean Stuber

select  * from yourtable
order by to_number(regexp_substr( tool_station_num,'[0-9]{1,2}$'))
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dustock

ASKER

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!