Never been to TextSnippets before?

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 (or not, you can keep them private!)

UDF to add items to a list, specifying the delimiter (See related posts)

--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

You need to create an account or log in to post comments to this site.


Related Posts