65.9K
CodeProject is changing. Read more.
Home

SQL: Column Values as Comma Separated String

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (6 votes)

Aug 10, 2013

CPOL
viewsIcon

104892

Column values as comma separated string in SQL

Introduction

I have a table as shown below, which has Country_Code and languages that are spoken by different people living in a country. I need to retrieve distinct languages spoken in a country based on the Country_Code column as a comma separated string.

SQL Statement

The following statement shows how it can be achieved:

declare @str varchar(1000)

SELECT @str= coalesce(@str + ', ', '') + a.CountryLang_Desc 
FROM (SELECT DISTINCT CountryLang_Desc from CountryLanguages where Country_Code='IN') a

print @str