Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Distinct comma delimited list

Tags:

sql

tsql

I am trying to create a comma delimted list of names in a table using the below query

DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + Name
FROM Production.Product
SELECT @listStr

This works fine, however the list does contain duplicates

Can anyone advise how I would make this DISTINCT so the list does not contain duplicates.

like image 225
level_zebra Avatar asked Jul 24 '13 11:07

level_zebra


1 Answers

Is it useful ?

DECLARE @listStr VARCHAR(MAX) 
SELECT @listStr = COALESCE(@listStr+',' ,'') + name 
FROM (SELECT DISTINCT name FROM Production.Product) t
SELECT @listStr
like image 148
Vinit Prajapati Avatar answered Oct 21 '22 19:10

Vinit Prajapati