SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Prepend Value to Delimited List


Prepend Value to Delimited List

Author
Message
Chad Casady
Chad Casady
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 50
Comments posted to this topic are about the item Prepend Value to Delimited List
paul_ramster
paul_ramster
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 534
In general, functions in SQL Server are a feature worth avoiding.
http://beyondrelational.com/quiz/sqlserver/tsql/2011/questions/Are-all-Scalar-User-Defined-Functions-UDF-always-bad.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+sqlquiz+%28SQL+Server+Quiz%29.

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

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 @ValueList
END


can be re-written as

select @Item + ISNULL(IsNull(@Delim,'') + @ValueList,'')



and for a simple piece of code like this, I would prefer to write out inline and avoid the overhead of calling a function.
Mitesh Oswal
Mitesh Oswal
SSC Eights!
SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)

Group: General Forum Members
Points: 964 Visits: 653




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

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




Regards,
Mitesh OSwal
+918698619998
Mitesh Oswal
Mitesh Oswal
SSC Eights!
SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)SSC Eights! (964 reputation)

Group: General Forum Members
Points: 964 Visits: 653


DECLARE @Numbertable table
(
ID INT PRIMARY KEY

)


INSERT INTO @Numbertable
select TOP 1000 ROW_NUMBER() OVER(order by si.object_id)
from sys.objects si,sys.objects s

DECLARE @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)

BEGIN
SELECT @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,'')

END
ELSE
BEGIN

INSERT INTO @separatolog

select distinct CHARINDEX(',',@str,ID)
from @Numbertable
where ID <= LEN(@str) and CHARINDEX(',',@str,ID) > 0
ORDER BY 1

DECLARE @CurrentSepartorID INT

SET @CurrentSepartorID = isnull((select SeparatorLogID
from @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,'')
END

select @Str




Regards,
Mitesh OSwal
+918698619998
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16082 Visits: 885
Thanks for the script. Nice continuation on the earlier one.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search