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

1

1 Answer 1

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.