0

In a SQL table there is a client_num column declared as char(5). When I sort the table by client_num with ASC it will have the typical ASCII sort.

1  
10    
100   
11  
110 

How to sort client_num as an INT while keeping the column still a type of char(5)?

EDIT

The above mentioned SQL order string as number has quite extensively answers. This question is simple and has straight answers. Though it is a duplicate. But it helps me very quickly.

5
  • 1
    try .. ORDER BY client_num+0 or .. ORDER BY CAST(client_num AS UNSIGNED) Commented Sep 18, 2016 at 7:12
  • doesn't work. What should +0 do ? Commented Sep 18, 2016 at 7:15
  • MySQL converts a string to numeric type when added zero like above. You could try the second option too. Commented Sep 18, 2016 at 7:18
  • Ok - I was to fast. It`s for casting AND it works perfect !!! Commented Sep 18, 2016 at 7:18
  • If you post an sep. answer I'll accept it. Commented Sep 18, 2016 at 7:20

1 Answer 1

1

You could cast the column to an integer:

SELECT   *
FROM     mytable
ORDER BY CAST(client_num AS INT) 
Sign up to request clarification or add additional context in comments.

5 Comments

Casting is the solution, yes. What is the difference here between UNSIGNED and INT ?
INT can be -ve, unsigned cannot
@Ben unsigned has only non-negative numbers, while ints can be negative.
well - stupid that question. So mySQl is finally doing the same cast.
To whom it may concern : CAST(client_num as INT) brings up a syntax error whlie CAST(client_num as UNSIGEND) not. Thats maybe a question of mySQL versions, don't know.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.