Using coalesce to build comma separated values as results from SQL Server
This article explain about how to Using coalesce to build comma separated values as results from SQL Server
This code will introduce you to a simple solution using 'Coalesce' in SQL Server for building comma separated strings.
--Sample table schema
Create table SchemaID
(
[ID] smallint,
SchemaID int NOT NULL
)
go
--Dummy insert statements
Insert into SchemaID values (1,12)
Insert into SchemaID values (1,13)
Insert into SchemaID values (1,14)
Insert into SchemaID values (2,15)
Insert into SchemaID values (2,16)
Insert into SchemaID values (2,17)
Insert into SchemaID values (2,18)
--Solution
Declare @ID varchar(100)
Select @id=Coalesce(@ID + ', ', '') + Cast(SchemaID AS varchar(5)) From SchemaID Where [ID] = 1
SELECT @ID
FYI, I have provided this as an solution to a question here - http://www.dotnetspider.com/qa/Question116526.aspx
Vadivel,
Can you kindly share scripts if you have any to create output like this
ID SchemaID
1 12,13,14
2 15,16,17,18