Click here to Skip to main content
15,887,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A table consists of field as contact_id its data type is setted as char(6). so while now in procedure if i order by with contact_id.
iam getting the output like this
1
11
12
.
.
.
2
21 like wise


but i need the output to be like this mention below

1
2
3 as so on.
Posted

SQL
select * from table order by convert(numeric(6,0),contact_id)

Happy Coding!
:)
 
Share this answer
 
Comments
ManojMurali 1-Apr-13 6:48am    
Thanks alot
Aarti Meswania 1-Apr-13 6:56am    
welcome!
Glad to help you! :)
 
Share this answer
 
This is why we tell people not to store numeric items as strings: the sort order is different, and there is no way to make a string sort "properly" without converting it to a number first.

The best solution is to change your DB so it stores numbers - it may seem like work now, but it saves a heck of a lot later. But you can do it, just - it's a pain and slow, but you can.
SQL
SELECT contact_id FROM myTable ORDER BY CONVERT(INT, contact_id)
However, if any of the contact_id values is not numeric, it will fail, and return no rows.
 
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