Click here to Skip to main content
15,889,527 members
Articles / Database Development / SQL Server
Tip/Trick

Get Table Column Value as Comma Separated Value (CSV) in SQL

Rate me:
Please Sign up or sign in to vote.
3.65/5 (6 votes)
9 Mar 2016CPOL 13.9K   6   2
Get the Comma Separated Value of one or multiple columns from SQL table

Introduction

The most common problem is to get the table column value as CSV (Comma separated value) in SQL and it gets more complex when we need to get it while getting information from multiple tables. So today, I am going to elaborate how we can get the CSV value with other information.

Using the Code

So for example, we have two tables as shown below. Create two tables by using the following SQL scripts.

SQL
CREATE TABLE [dbo].[TableA](
    [IDA] [int] IDENTITY(1,1) NOT NULL,
    [NameA] [varchar](50) NULL,
 CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED 
(
    [IDA] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
	ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


CREATE TABLE [dbo].[TableB](
	[IDB] [int] IDENTITY(1,1) NOT NULL,
	[NameB] [varchar](50) NULL,
	[IDA] [nchar](10) NULL,
 CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED 
(
	[IDB] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
	ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Use the below data scripts to add data in these above tables, it is just for the ease, otherwise you can have your own data values as well.

SQL
SET IDENTITY_INSERT [dbo].[TableB] ON
INSERT [dbo].[TableB] ([IDB], [NameB], [IDA]) VALUES (1, N'NameB1', N'3         ')
INSERT [dbo].[TableB] ([IDB], [NameB], [IDA]) VALUES (2, N'NameB2', N'3         ')
INSERT [dbo].[TableB] ([IDB], [NameB], [IDA]) VALUES (3, N'NameB3', N'4         ')
INSERT [dbo].[TableB] ([IDB], [NameB], [IDA]) VALUES (4, N'NameB3', N'4         ')
INSERT [dbo].[TableB] ([IDB], [NameB], [IDA]) VALUES (5, N'NameB4', N'4         ')
SET IDENTITY_INSERT [dbo].[TableB] OFF
/****** Object:  Table [dbo].[TableA]    Script Date: 03/09/2016 14:45:56 ******/
SET IDENTITY_INSERT [dbo].[TableA] ON
INSERT [dbo].[TableA] ([IDA], [NameA]) VALUES (1, N'Name1')
INSERT [dbo].[TableA] ([IDA], [NameA]) VALUES (2, N'Name2')
INSERT [dbo].[TableA] ([IDA], [NameA]) VALUES (3, N'Name3')
INSERT [dbo].[TableA] ([IDA], [NameA]) VALUES (4, N'Name4')
INSERT [dbo].[TableA] ([IDA], [NameA]) VALUES (5, N'Name5')
SET IDENTITY_INSERT [dbo].[TableA] OFF

Then the final output will be like below:

Table A
IDA

NameA
Table B
IDB

NameB

IDA
1 Name1 1 NameB1 3
2 Name2 2 NameB2 3
3 Name3 3 NameB3 4
4 Name4 4 NameB4 4
5 Name5 5 NameB5 4

There are two very common ways to get the value as comma separated:

SQL
Declare @Names Varchar(8000)
Select @Names= COALESCE(@Names + ',','') + a.NameA
From tableA a join tableB b on a.IDA = b.IDA
Print @Names

Output : csv2

SQL
With CTE_CSV (NameA)
AS
(
Select SUBSTRING((Select ',' + NameA From TableA
For XML Path('')),2,10000) as CSV
)
Select NameA From CTE_CSV

Output : csv

Hope this small tip with a simple approach will help others.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) TECNEX System LLC
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionUse STUFF instead of SUBSTRING Pin
Muhammad Shahid Farooq9-Mar-16 19:15
professionalMuhammad Shahid Farooq9-Mar-16 19:15 
GeneralRe: Use STUFF instead of SUBSTRING Pin
BeaglesEnd9-Mar-16 22:13
BeaglesEnd9-Mar-16 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.