Pages

Saturday, September 4, 2010

SQL Server - Comma delimited string from a table column

This is a very simple and efficient way of getting values of a column in a delimited string.


USE AdventureWorks 
GO 


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


SELECT @nameslist


Using the COALESCE function ensures that result set does not get affected by null values. For more information about this function check out this link.

No comments:

Post a Comment