Link to home
Start Free TrialLog in
Avatar of ronipats
ronipats

asked on

sql server 2008, stored procedure

I have one table in sqlserver2008. with two fields, name and rollno.

name varchar(100),
rollno varchar(50)

though i want to store numbers in rollno i have kept it as varchar(50). now my requiement is i want to sort all records based on rollno just like numbers.

say eg.

rollno
1
2
3
4
5
6
7
8
9
10
11

instead of
1
10
11
2
3
.....

how do i do that using sql qurey. Is ther any way to sort text fields just like numbers.
thanks
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Select name, rollno
from mytable
order by cast(rollno as int)
The above answer is correct but you may also do it as below

select Name,CONVERT(INT, rollno) rollno from mytable order by 2
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial