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 VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
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
SQL 2016 DESIGN & PROGRAMMING
 
 
 
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