Posted by: NepaliBhai May 31, 2007
Login in to Rate this Post:
0
?
I have a sql question. Please take a look at the table image.
I need to have result table with additional field that has concatinated value of Extra1, Extra2, Extra3 when name, address and state match.
I have written this way but did not get correct result.
select * from testtable order by name
drop table #table1
SELECT *,
case when (rtrim(Extra1) is not null and rtrim(Extra1) <>'') then rtrim(Extra1) else '' end +
case when (rtrim(Extra2) is not null and rtrim(Extra2) <>'') then ',' + rtrim(Extra2) else '' end +
case when (rtrim(Extra3) is not null and rtrim(Extra3) <>'') then ','+ rtrim(Extra3) else '' end as Concatinated
into #table1
FROM testtable y
select * from #table1 order by name
update #table1 set Concatinated = rtrim(y.Extra1) + ',' + rtrim(y.Extra2) + ',' + rtrim(y.Extra3)
from testtable y, #table1 x
where rtrim(y.name) = rtrim(x.name) and rtrim(y.address) = rtrim(x.address) and rtrim(y.state) = rtrim(x.state)
select * from #table1 order by name