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 2005 Forums
 Transact-SQL (2005)
 Concatenate string

Author  Topic 

Corobori
Posting Yak Master

105 Posts

Posted - 2008-07-25 : 13:09:39
I am trying to change the code shown below to concatenate string

Original


declare @string varchar(500)
set @string = 'ABC,DEF,GHIJK,LMNOPQRS,T,UV,WXY,Z'


declare @pos int
declare @piece varchar(500)

-- Need to tack a delimiter onto the end of the input string if one doesn't exist
if right(rtrim(@string),1) <> ','
set @string = @string + ','

set @pos = patindex('%,%' , @string)
while @pos <> 0
begin
set @piece = left(@string, @pos - 1)

print cast(@piece as varchar(500))

set @string = stuff(@string, 1, @pos, '')
set @pos = patindex('%,%' , @string)
end


Mine

declare @string varchar(500)
set @string = 'ABC,DEF,GHIJK,LMNOPQRS,T,UV,WXY,Z'


declare @pos int
declare @piece varchar(20)
declare @swhere varchar(4000)

-- Need to tack a delimiter onto the end of the input string if one doesn't exist
if right(rtrim(@string),1) <> ','
set @string = @string + ','

set @pos = patindex('%,%' , @string)
while @pos <> 0
begin
set @piece = left(@string, @pos - 1)


set @swhere = @swhere + ' ' + @piece

set @string = stuff(@string, 1, @pos, '')
set @pos = patindex('%,%' , @string)
end

print (@swhere)


I can't figure out why @swhere is empty

jean-luc
www.corobori.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-25 : 13:46:35
you have not intialised @swhere.change
set @swhere = @swhere + ' ' + @piece
to
set @swhere = COALESCE(@swhere + ' ','') + @piece
Go to Top of Page

Corobori
Posting Yak Master

105 Posts

Posted - 2008-07-25 : 14:16:04
quote:
Originally posted by visakh16

you have not intialised @swhere.change
set @swhere = @swhere + ' ' + @piece
to
set @swhere = COALESCE(@swhere + ' ','') + @piece



Solved, thanks

jean-luc
www.corobori.com
Go to Top of Page
   

- Advertisement -