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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Odd Looking String Concatenation

Author  Topic 

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-03-03 : 17:43:03
This isn't a question, I just wanted to post this odd looking query result for fun.
declare @num table (NUMBER varchar(20) not null primary key clustered)

insert into @num (NUMBER)
select n from (
select n=0 union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all
select 9 ) n

declare @s1 varchar(25)
declare @s2 varchar(25)
select @s1 = '', @s2 = ''

select
@s1 = @s1 + NUMBER + '_' ,
@s2 = '_' + @s2 + NUMBER
from
@num
order by
NUMBER

select S1 = @s1, S2 = @s2
Produces these results:
S1                        S2                        
------------------------- -------------------------
0_1_2_3_4_5_6_7_8_9_ __________0123456789

I was very puzzled at first by why all the underscores end up at the beginning of string S2, but when I really thought it through, I saw it really is correct.

Is the reason instantly apparent to everyone else? Am I the only slow one?



CODO ERGO SUM

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-03-03 : 17:54:51
Took me a few minutes, so you're not the only slow one



Damian
Go to Top of Page
   

- Advertisement -