1

I have this problem. A column in MySQL DB has data as following: 1 10 100 102 1020 1022 1023 1024 1025 1025A 1026A . . .

I Obtained correct numeric sorting with a query like this:

    SELECT DISTINCT LPAD(ubicazione, 8, '0') FROM t_ubicazione ORDER BY LPAD(ubicazione, 8, '0') ASC;

that give me this result:

00000001 00000017 00000018 00000019 0000001A 0000001B 0000001M 0000001S 00000021 00000022

My problem is that 0000001A should be after 00000001, so correct order should be:

00000001
0000001A
0000001B
0000001M
0000001S
00000017
00000018
00000019
00000021
00000022

how can I obtain this result?

2 Answers 2

1

You can get the numeric part of the column as an integer number with an implicit conversion ubicazione + 0 and sort by that first and then ubicazione:

SELECT ubicazione 
FROM t_ubicazione 
ORDER BY ubicazione + 0, ubicazione;

See the demo.

Sign up to request clarification or add additional context in comments.

Comments

0

SELECT ubicazione FROM t_ubicazione ORDER BY CAST(ubicazione AS UNSIGNED), ubicazione

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.