SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 STUFF2
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

waterduck
Aged Yak Warrior

Malaysia
962 Posts

Posted - 10/04/2013 :  03:12:21  Show Profile  Reply with Quote
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

Edited by - waterduck on 10/04/2013 04:44:03

webfred
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 10/04/2013 :  03:44:52  Show Profile  Visit webfred's Homepage  Reply with Quote
Hi Waterduck,

please give us a few words on
when/why to use
how to use
etc.

Thx
Webfred


Too old to Rock'n'Roll too young to die.
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
962 Posts

Posted - 10/04/2013 :  04:43:31  Show Profile  Reply with Quote
Sorry, forgot the example.

It meant for insert character/string into multiple location.


SELECT
       Original = 'abcdefghijlkmnop',
       Modified = dbo.STUFF2('abcdefghijlkmnop', '6,3, 9, 16', 0, ':')




before - abcdefghijlkmnop
after - abc:def:ghi:jlkmnop:
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
962 Posts

Posted - 10/04/2013 :  04:44:31  Show Profile  Reply with Quote
any better idea are highly welcome with a beer =P
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
962 Posts

Posted - 10/07/2013 :  21:33:20  Show Profile  Reply with Quote
links for fnParseList
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000