Wednesday, February 23, 2011

[Tips] Create a comma-separated list in one query using COALESCE

Clever, clever. Wish I could take credit for it.


-----------------------------------
--Creating a comma-separated list--
-----------------------------------

DECLARE @EmployeeList varchar(100)

SELECT @EmployeeList = COALESCE(@EmployeeList + ', ', '')
+ CAST(id AS varchar(15))
FROM temp_td_200704

SELECT @EmployeeList

No comments: