Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends.

i have a table that i have more than 20 rows on it. i have a query for fetching 20 rows from this table that have a same column. and this 20 rows have a column that for every row it has a value from 1 to 20. for example first row has column 'radif' that its value is 6, second rows radif column has value of 13 and others like this. i want to select a row that has a maximum value of radif column. i do not know maximum number of radif and do not know where it is placed. i tested this my self for 20 rows and with a 'order by radif asc' command ssms return numbers in this order
1-10-11-12-13-14-15-16-17-18-19-2-3-4-5-6-7-8-9.i want to ssms return radif in this format-->
1-2-3-4-5-6-...
how can i do that?please help me.
thanks in advance.
Posted
Comments
[no name] 3-Jul-14 11:16am    
You would need to convert your string to a number to get an ordinal sorting.
_Starbug_ 3-Jul-14 11:33am    
thanks.good idea.

1 solution

The trouble is that you have stored your numer as a string - so it uses a strign comparision for everything, and that works on a character-by-character basis. The firts character that is differnet decides the comparison.

The best solution is never to store numeric data as strings; use a numeric field. That way comparisons work properly, and you can useful things like math operations on them.

Failing that, you'd need to convert the strign to an number - and hope like heck that none of them contain "bad" characters...
SQL
...ORDER BY CAST(radif AS INT) ASC
Will do it temporarily, but seriously - change your DB!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900