Unexpected behaviour of Order by clause(SQL SERVER 2005)
        Posted  
        
            by Newbie
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Newbie
        
        
        
        Published on 2010-05-24T03:08:32Z
        Indexed on 
            2010/05/24
            3:10 UTC
        
        
        Read the original article
        Hit count: 234
        
sql-server-2005
I have a table which looks like
Col1    col2 col3 col4 col5
1 5 1 4 6
1 4 0 3 7
0 1 5 6 3
1 8 2 1 5
4 3 2 1 4
The script is
declare @t table(col1 int, col2 int, col3 int,col4 int,col5 int)
insert into @t 
select 1,5,1,4,6 union all
select 1,4,0,3,7 union all
select 0,1,5,6,3 union all
select 1,8,2,1,5 union all
select 4,3,2,1,4
If I do a sorting (ascending), the output is
Col1 col2 col3 col4 col5
0 1 5 6 3
1 4 0 3 7
1 5 1 4 6
1 8 2 1 5
4 3 2 1 4
The query is
Select * from @t
order by col1,col2,col3,col4,col5
But as can be seen that the sorting output is wrong (col2 to col5).
Why so and how to overcome this?
Thanks in advance
© Stack Overflow or respective owner