How to use?
SELECT
Original = 'abcdefghijlkmnop',
Modified = dbo.STUFF2('abcdefghijlkmnop', '6,3, 9, 16', 0, ':')
|
Output
Query
CREATE FUNCTION [dbo].[STUFF2]
(
@String nvarchar(max),
@Location nvarchar(max),
@Occurance int,
@Delimeter nvarchar(max)
)
RETURNS nvarchar(max)
BEGIN
DECLARE @Output nvarchar(max) = ''
SELECT @Output = CASE WHEN Data > LEN(@String) THEN
@Output + @Delimeter
ELSE
STUFF(CASE WHEN RN = 1 THEN
@String ELSE @Output END, Data + RN - 1, @Occurance, @Delimeter)
END
FROM (SELECT Data = CAST(Data AS INT) + CASE WHEN @Occurance = 0 THEN
1 ELSE 0 END,
RN = ROW_NUMBER() OVER (ORDER BY LEN(Data), Data)
FROM dbo.fnParseList(',', @Location)
)Src
ORDER BY RN
RETURN @Output
END
|
fnParseList by Peter
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033
No comments:
Post a Comment