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.

Ben
  • 677
  • 5
  • 19

1 Answers1

1

You could cast the column to an integer:

SELECT   *
FROM     mytable
ORDER BY CAST(client_num AS INT) 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Casting is the solution, yes. What is the difference here between `UNSIGNED` and `INT` ? – Ben Sep 18 '16 at 07:21
  • INT can be -ve, unsigned cannot – Ed Heal Sep 18 '16 at 07:22
  • 1
    @Ben `unsigned` has only non-negative numbers, while `int`s can be negative. – Mureinik Sep 18 '16 at 07:23
  • well - stupid that question. So mySQl is finally doing the same cast. – Ben Sep 18 '16 at 07:33
  • 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. – Ben Sep 18 '16 at 07:47