0

I have a "Location" data set returned by a simple query from a MySQL database:

A1
A10,
A2
A3

It is sequenced by an "Order By Location" statement. The issue is that I would like the returned sequence to be:

A1
A2
A3
A10

I am not sure if this is achievable with a MySQL Order By statement?

3
  • you alwqays have A value in begining ? Commented Mar 11, 2014 at 22:06
  • no, not always, however the others are B and C Commented Mar 12, 2014 at 2:49
  • ok look my edited answer. Commented Mar 12, 2014 at 8:29

3 Answers 3

2

I think the easiest way to do this is to order by the length and then the value:

order by length(location), location
Sign up to request clarification or add additional context in comments.

1 Comment

taking length into account may work for the example given - But if there is a location like A001 it will fail. However, the provided examples do not clearly mention all possible variations.
1

try this

order by CAST(replace((Location),'A','') as signed )

DEMO HERE

EDIT:

if you have other letters then A then consider to cut the first letter and order the rest as integers.

    ORDER BY CAST(SUBSTR(loc, 2) as signed ) 

DEMO HERE

1 Comment

This has been really helpful. In the end the following worked perfectly: ORDER BY SUBSTR(Loc, 1,1), CAST(SUBSTR(Loc, 2, 2) as signed )
1

Try

ORDER BY SUBSTR(location, 2)

1 Comment

@echo_Me I`ve read in my horoscope that as soon as I hit 7000,my salary will be doubled,my girlfriend will be twice as beautiful and North Korea will renounce nuclear weapons.So it will be a very important event,no doubt.

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.