Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sorting of a string column

Author  Topic 

SoniaGupta
Starting Member

1 Post

Posted - 2008-03-07 : 06:10:15
I have the column of type string in the database
Following is the data in that column
1
2
11
12
21
abc
If i sort the table with the help of this column then the output come in the following manner
1
11
12
2
21
abc

can i improve the order actually i want the output like 1 2 11 12 21 abc

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-07 : 08:07:11
Always use proper datatype to store data

Use one of these

1 Order by cast(col as int)
2 order by len(col),col
3 order by col*1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-07 : 09:11:07
Madhivanan -- he's got some alphas in there, too, it looks like.

One way is to pad it with leading zeroes or spaces and then sort

order by right('0000000000' + col, 10)

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -