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

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

UDF to add a sql_variant to a list

--Adds a sql_variant element to the end of a sql_variant list, after first inserting a delimiter (nvarchar)
--If both element and list are nvarchars, using dbo.fnAddToList will be faster
ALTER FUNCTION dbo.fnAddVarToList (@VarList sql_variant, @VarNew sql_variant, @Del nvarchar(10))
RETURNS nvarchar(4000)
AS  
BEGIN 
        DECLARE @List nvarchar(4000), @New nvarchar(4000)
        SELECT @List = NULLIF(CONVERT(nvarchar(4000), @VarList), ''), 
                @New = NULLIF(CONVERT(nvarchar(4000), @VarNew), '')
        --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

UDF to add items to a list, specifying the delimiter

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

UDF to Add items to a comma delimited list

/*
_A_dd to _C_omma delimited _L_ist - simplified version of AddToList
Adds an element (nvarchar) to the end of a comma delimited list (nvarchar)
*/
ALTER FUNCTION dbo.fnACL (@List nvarchar(4000), @New nvarchar(4000))
RETURNS nvarchar(4000)
AS  
BEGIN 
        --Treat ''s as NULLs
        SELECT @List = NULLIF(@List, ''), @New = NULLIF(@New, '')
        --first try returning the concatened string, if null then try just the list, if it too is null, just the new element
        RETURN COALESCE(@List + ',' + @New, @List, @New)
END
« Newer Snippets
Older Snippets »
3 total  XML / RSS feed