﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Chad Casady  / Prepend Value to Delimited List / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 00:29:18 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Prepend Value to Delimited List</title><link>http://www.sqlservercentral.com/Forums/Topic1075282-2913-1.aspx</link><description>[code="sql"]DECLARE @Numbertable table(	ID INT PRIMARY KEY	)INSERT INTO @Numbertableselect TOP 1000 ROW_NUMBER() OVER(order by si.object_id)from sys.objects si,sys.objects sDECLARE @separatolog TABLE(	SeparatorLogID INT,	ID INT IDENTITY(1,1) PRIMARY KEY)DECLARE @Str varchar(100) = 'FN,SP,TBL,091.8891'DECLARE @Separator VARCHAR(10) = ','DECLARE @ValueAdd VARCHAR(10)  = '80990'DECLARE @Action VARCHAR(10)  = '3'--1. P..PostFix  2. R..Prefix 3.Number for postion.IF (ISNUMERIC(@Action) =0) BEGINSELECT @Str = STUFF((        SELECT                 CASE WHEN @Action = 'R' THEN ISNULL(@Separator+@ValueAdd,'') ELSE '' END        +        ISNULL(@Separator+@Str,'')        +        CASE WHEN @Action = 'P' THEN ISNULL(@Separator+@ValueAdd,'') ELSE '' END),1,1,'')ENDELSEBEGININSERT INTO @separatologselect distinct CHARINDEX(',',@str,ID)from @Numbertablewhere ID &amp;lt;= LEN(@str) and CHARINDEX(',',@str,ID) &amp;gt; 0ORDER BY 1DECLARE @CurrentSepartorID INT SET @CurrentSepartorID = isnull((select  SeparatorLogIDfrom @separatolog where ID = CAST(@Action AS INT)-1),CASE WHEN @Action = 1 THEN 0 ELSE LEN(@str)+1 END)select @Str = STUFF(SUBSTRING(ISNULL(@Separator+@str,''),1,@CurrentSepartorID)			  +ISNULL(@Separator+@ValueAdd,'')			  +ISNULL(@Separator+NULLIF(SUBSTRING(@str,@CurrentSepartorID+1,LEN(@str)),''),''),1,1,'')ENDselect @Str [/code]</description><pubDate>Wed, 27 Apr 2011 03:32:32 GMT</pubDate><dc:creator>Mitesh Oswal</dc:creator></item><item><title>RE: Prepend Value to Delimited List</title><link>http://www.sqlservercentral.com/Forums/Topic1075282-2913-1.aspx</link><description>[code="sql"]DECLARE @Str varchar(100) = 'FN,SP,TBL,091.889'DECLARE @Separator VARCHAR(10) = ','DECLARE @ValueAdd VARCHAR(10)  = '80990'DECLARE @Action VARCHAR(10)  = 'R'--1. P..PostFix  2. R..PrefixSELECT @Str = STUFF((	SELECT 		CASE WHEN @Action = 'R' THEN ISNULL(@Separator+@ValueAdd,'') ELSE '' END	+	ISNULL(@Separator+@Str,'')	+	CASE WHEN @Action = 'P' THEN ISNULL(@Separator+@ValueAdd,'') ELSE '' END),1,1,'')select @Str[/code]</description><pubDate>Wed, 27 Apr 2011 03:03:31 GMT</pubDate><dc:creator>Mitesh Oswal</dc:creator></item><item><title>RE: Prepend Value to Delimited List</title><link>http://www.sqlservercentral.com/Forums/Topic1075282-2913-1.aspx</link><description>In general, functions in SQL Server are a feature worth avoiding.[url]http://beyondrelational.com/quiz/sqlserver/tsql/2011/questions/Are-all-Scalar-User-Defined-Functions-UDF-always-bad.aspx?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+sqlquiz+%28SQL+Server+Quiz%29[/url].The functions you suggest are probably the "best" or maybe "least-worst", in that they are scalar functions that access no database tables.  They do require a small overhead over writing out the function inline, but on the other hand, they promote code-reuse and make the code easier to read.However, the contents of your function [code="sql"]BEGIN    -- If the delimiter passed was null, use an empty string.    set @Delim = IsNull(@Delim, '')    -- If the list is empty, set it equal to the value that was passed in.    if (@ValueList IS NULL)    begin        set @ValueList = @Item    end    -- If the item passed in was not null, append it to the list.    else if (@Item IS NOT NULL)    begin        set @ValueList = @Item + @Delim + @ValueList    end    RETURN @ValueListEND[/code]can be re-written as[code="sql"]select @Item + ISNULL(IsNull(@Delim,'') + @ValueList,'')[/code]and for a simple piece of code like this, I would prefer to write out inline and avoid the overhead of calling a function.</description><pubDate>Wed, 09 Mar 2011 02:38:52 GMT</pubDate><dc:creator>paul_ramster</dc:creator></item><item><title>Prepend Value to Delimited List</title><link>http://www.sqlservercentral.com/Forums/Topic1075282-2913-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/UDF/72558/"&gt;Prepend Value to Delimited List&lt;/A&gt;[/B]</description><pubDate>Tue, 08 Mar 2011 21:08:59 GMT</pubDate><dc:creator>Chad Casady</dc:creator></item></channel></rss>