Wednesday, December 7, 2016

Concatenating Column Values into a Comma-Separated List into SQL Server

Here i am telling about concatenating column value into comma-separated list as below:

Declare @AllEmail AS VARCHAR(8000),@Position int=3
Declare @t table (ID int identity,EmailID nvarchar(200))
Insert into @t(EmailID)
Select distinct(EmailID) From Employee Where 
Position=@Position AND IsActive=1 AND IsDeleted=0
SELECT @AllEmail = ISNULL(@AllEmail +';','') + EmailID FROM @t
SELECT @
AllEmail 
Here you get output at resultset as comma separated email id. 

No comments:

Post a Comment