Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Add Variant To List

DZone's Guide to

Add Variant To List

·
Free Resource
If both element and list are nvarchars, using dbo.fnAddToList will be faster

Oskar Austegard
http://mo.notono.us

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}