Nikhil
Nikhil

Reputation: 71

how to replace a word first occurrence from string in sql

i have a input

Input :- Or Like %Gifts%Or Like % Packaging%

Expected Output:- %Gifts%Or Like % Packaging%

How to achieve this in sql

Upvotes: 0

Views: 98

Answers (1)

Squirrel
Squirrel

Reputation: 24793

this is what you want ? replace 1st occurance of the word Or Like ?

declare @str    varchar(100) = 'Or Like %Gifts%Or Like % Packaging%',
        @word   varchar(10)  = 'Or Like'

select  [output]    = case  when charindex(@word, @str) > 0
                            then stuff(@str, charindex(@word, @str), len(@word), '')
                            else @str
                            end

You may use charindex() to find the 1st occurance of the string Then remove that string by stuff() it with empty string

Upvotes: 1

Related Questions