1

I have a column with values as below

18 ABC
45 XYZ
1 ABC
83 DEF
22 XYZ
4 ABC

I want them to be sorted as below while pulling the values from oracle DB

1 ABC
4 ABC
18 ABC
22 XYZ
45 XYZ
83 DEF

when I use order by substr(column,1) it is not giving the result as expected 4 ABC will go down after 22 XYZ

3
  • 2
    Which DBMS are you using? Postgres? Oracle? Commented Jun 26, 2013 at 12:33
  • Wouldn't that result be expected as substr('22 XYZ',1) should give 2 and thus be sorted before 4? Commented Jun 26, 2013 at 12:36
  • 3
    Fix the table might be the best answer. Commented Jun 26, 2013 at 12:37

2 Answers 2

2

You may do the ff:

order by to_number(substr(column, 1, instr(column, ' ')))
Sign up to request clarification or add additional context in comments.

2 Comments

it worked, thanks but what if there is no space between the number and the word like 4ABC ?
I can give a number instead of instr if I know how many digits it would be, but this may change over the time..hmmm
0

Did you try something like ORDER BY CAST(substr(column, 2) AS INT)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.