sql server - Sql concatenation not working with case statement in sql -
i want concatenate multiple rows single text string checking few conditions using case statement. query returns last value rather concatenating values.
my sql query below
declare @str varchar(1000) set @str = '' select @str = @str + case when substring(value,charindex('|',value)+1,len(value)) <> '' '$' +replace(value,'|',' - ') else '$' +isnull(replace(value,'|',''),'') end + ', ' dbo.sometable print @str
the common way row concatenation in sql server use for xml path
select @str = stuff((select case when substring(value,charindex('|',value)+1,len(value)) <> '' '$' +replace(value,'|',' - ') else '$' +isnull(replace(value,'|',''),'') end @t xml path('')), 1, 1, '')
Comments
Post a Comment