Import Comma Separated Data into Single Column

Introduction

This blog describes how to get Comma Separated data from the table. Content will be added soon. If you are familiar with this, please consider adding any troubleshooting tips that may help the community.

Created one example table

  1. CREATE TABLE Articles  
  2. (  
  3.  Title VARCHAR(50),  
  4.  ArticleType VARCHAR(10),  
  5.  Tags VARCHAR(500)  
  6. )  
Insert data in table
  1. insert into Articles values('test article title-01','C','C')  
  2. insert into Articles values('test article title-01','C','C++')  
Created function to take Events with Comma Separated on Event Type basis
  1. CREATE FUNCTION dbo.MakeCommaValue (@pC2 AS VARCHAR(10))  
  2.         RETURNS VARCHAR(8000)  
  3. AS  
  4. BEGIN  
  5.         DECLARE @oResult VARCHAR(8000)  
  6.         SELECT @oResult= COALESCE(@oResult+',','')+CAST(Events AS VARCHAR(10))  
  7.         FROM Articles WITH (NOLOCK)  
  8.         WHERE ArticleType = @pC2  
  9.         ORDER BY Tags  
  10.         RETURN @oResult  
  11. END  
  12. --Call the Function like this  
  13. SELECT DISTINCT Title,ArticleType,dbo.MakeCommaValue('B'as 'Tags' FROM Articles  
Result

 

Title ArticleType Tags
test article title-01 c C, C++
Ebook Download
View all
Learn
View all