Monday, May 7, 2012

SQL - Return Multiple Records on One Line

If you have multiple records you want to appear on one line delimited by some character such as a comma, see below.

This would make:

Jones
Smith
Williams

Look like this:

Jones, Smith, Williams




DECLARE @all_names varchar(2000);

SELECT @all_names = COALESCE(@all_names + ', ', '') +
a.last_name 
FROM customers_table a
--WHERE .... (this is optional)

SELECT @all_names as 'last_names'

No comments:

Post a Comment