I was trying to concatenate values by comma when I will use group by. So I use STUFF() function and for xml path('') clause. Here is my script
IF EXISTS
(
SELECT *
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(N'tempdb..#tmp1')
)
BEGIN
DROP TABLE #tmp1
END
create table #tmp1
(ID varchar(2),CName varchar(20) )
insert into #tmp1(ID,CName) values ('A','BBA Reman')
insert into #tmp1(ID,CName) values ('B','BBA Reman')
insert into #tmp1(ID,CName) values ('C','CT Tech')
insert into #tmp1(ID,CName) values ('D','CT Tech')
select
t1.CName,
stuff((
select ',' + t.ID
from #tmp1 t
where t.CName = t1.CName
order by t.ID
for xml path('')
),1,1,'') as ConCatStr
from #tmp1 t1
group by t1.CName
The above script run successfully in SQL Server 2005 but not running in SQL Server 2000. So tell me how to restructure my script as a result it should run in SQL Server 2000 also.
there are a lot of methods to create the comma separated list without using xml, take a look at the following:
http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/