DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to create a comma-delimited list with COALESCE?

The following Microsoft SQL Server T-SQL sample scripts demonstrate the usage of COALESCE for list generation:

USE AdventureWorks2008;

DECLARE @ColorList varchar(100)

 

SELECT  @ColorList = COALESCE(@ColorList + ', ', '') + Color

FROM (SELECT DISTINCT Color FROM Production.Product

WHERE Color is not NULL ) x

 

SELECT ProductColors = @ColorList

GO

/* ProductColors

Black, Blue, Grey, Multi, Red, Silver, Silver/Black, White, Yellow

*/

 

USE pubs

GO

 

DECLARE  @Publishers VARCHAR(4096)

 

SELECT @Publishers = COALESCE(@Publishers + ', ','') + pub_name

FROM   publishers

 

SELECT Publishers = @Publishers

 

GO

/*

Publishers

New Moon Books, Binnet & Hardley, Algodata Infosystems, Five Lakes Publishing,

Ramona Publishers, GGG&G, Scootney Books, Lucerne Publishing

*/

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE