Question : SQL awkward behaviour (urgent)

Hi  All

Below I have attached the code where im getting some problem.

This will first create a temp table named #t1 with one column c1 with data as follows:
3A
1B
2C

I need to get comma seperated string with order of numeral part (excluding last char), so I cast it like cast(SUBSTRING(c1, 0, len(c1)) as int) in order by clause.

Please run the attached query.
Now the result Im getting is a bit awkward.

@var returns '3A,'   but I was expecting '1B,2C,3A'.

Could some body tell me why such a behaviour.
Also let me know the alternate approach for this.
Please help.
 
Thanks
Vipin Goel
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
create table #t1 (c1 varchar(5))

insert into #t1 values ('3A')
insert into #t1 values ('1B')
insert into #t1 values ('2C')

declare @var varchar(100)
set @var = ''

select  @var = @var + c1 + ',' 
from #t1
order by cast(SUBSTRING(c1, 0, len(c1)) as int) 

select * from #t1

select cast(c1 as varchar)
from #t1
order by cast(SUBSTRING(c1, 0, len(c1)) as int) 

select @var

drop table #t1

Answer : SQL awkward behaviour (urgent)

Hi,

please check this
1:
2:
3:
4:
select  @var = @var + c1 + ',' 
from #t1
group by c1, cast(SUBSTRING(c1, 1, len(c1)-1 ) as int) 
order by cast(SUBSTRING(c1, 1, len(c1)-1 ) as int)
Random Solutions  
 
programming4us programming4us