Click here to Skip to main content
15,888,340 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear sir,


i wanted find particular pattern in string and replace the same with another pattern
for example:

string='a,b,c: d, e, f :'

findpattern :

replacepattern: ,:

output will be a,b,c,: d, e, f ,:

What I have tried:

Alter FUNCTION dbo.RecursiveReplace_fn1
(
    @P_String VARCHAR(MAX),
    @P_Pattern VARCHAR(MAX),
    @P_ReplaceString VARCHAR(MAX),
    @P_ReplaceLength INT = 1
)
RETURNS VARCHAR(MAX)
BEGIN
    DECLARE @Index INT;

    -- Get starting point of pattern
    SET @Index = PATINDEX(@P_Pattern, @P_String);

    IF @Index > 0
    BEGIN
        -- Perform the replace
        SET @P_String = STUFF(@P_String, PATINDEX(@P_Pattern, @P_String), @P_ReplaceLength, @P_ReplaceString);

        -- Recurse
        SET @P_String = dbo.RecursiveReplace_fn1(@P_String, @P_Pattern, @P_ReplaceString, @P_ReplaceLength);
    END;
    
    RETURN @P_String;
END;
Posted
Updated 30-Oct-18 8:34am
v2
Comments
[no name] 11-Aug-18 8:17am    
And you really like to do this recursive?
Another Thing: Think about the fact that your replace pattern contains the find pattern.
Patrice T 11-Aug-18 12:04pm    
Do you have a question ?

1 solution

When you asked a very similar question 7 hours ago: Sql fuction manipulate string dynamically may passing parameter string needs to add any character in string given position[^] I told you that your whole approach was wrong: do you now start to see why your whole storage desiogn is faulty?

This problem won't get any better - it's going to get worse instead. Do yourself a favour and change your DB before you get so far down the road that it becomes a huge PITA.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900