DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Add To List Function

01.18.2006
| 5035 views |
  • submit to reddit
        
--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
Oskar Austegard
<a href="http://mo.notono.us">http://mo.notono.us</a>