2

I'm using SQL Server 2008 and would like to have a user defined function that concatenates the values of a single column, comma delimited, and returns it.

For example, a table with the following rows:

column1
---------
head
shoulders
knees
toes

The function would return the single value: head, shoulders, knees, toes

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
BeYourOwnGod
  • 2,345
  • 7
  • 30
  • 35
  • 1
    If you know how to write a UDF, http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string should be all you need to know. – ta.speot.is Aug 13 '11 at 06:40

1 Answers1

3

UDF version of Concatenate many rows into a single text string?

CREATE FUNCTION [dbo].[udf_GetNumDaysInMonth] ()
RETURNS nvarchar(4000)
AS
BEGIN
   DECLARE @str VARCHAR(4000) 
   SELECT @str= COALESCE(@str + ', ', '') + column_name FROM tablename ORDER BY column_name
   return @str
END
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216