--Adds an element (nvarchar) to the end of a list (nvarchar), after first inserting a delimiter (nvarchar) ALTER FUNCTION dbo.fnAddToList (@List nvarchar(4000), @New nvarchar(4000), @Del nvarchar(10)) RETURNS nvarchar(4000) AS BEGIN --Treat ''s as NULLs SELECT @List = NULLIF(@List, ''), @Del = NULLIF(@Del, ''), @New = NULLIF(@New, '') --First try the concatened string, if null then just the list, --if it too is null, just the new element RETURN COALESCE(@List + @Del + @New, @List, @New) END