0

I'm wondering if there is a way to do a numeric sort when the number is a string. Take a look at example:

The query:

SELECT * FROM t_tables t order by id_string asc

This is the list from DB:

13
4
6
8
10
1
3
2
5
12
7
9
11

I need to extract it in this way:

1
2
3
4
5
6
7
8
9
10
11
12
13

But as result I get this:

1
10
11
12
13
2
3
4
5
6
7
8
9

3 Answers 3

2

If you know that you always have numeric values in the column you can use the CAST() operator. Also have a look at this question.

SELECT * FROM t_tables t ORDER BY CAST(id_string AS UNSIGNED) ASC
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT * FROM t_tables t order by cast(id_string as signed integer) asc;

Comments

1

Hope it helps:

SELECT * FROM t_tables t order by cast(id_string as unsigned)

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.