2
select REPLACE(comments,substring(comments, 
charindex('statement',comments), LEN(comments)),'') 
from Customer 
where charindex('statement',comments) <>0

I want to replace only "statement" from column comments using Replace function but it replaces records to ' ' which doesn't have "Statement" string in it.

2 Answers 2

3

Pls try like this, this will only replace the string statement from the comments :

declare @Customer table(comments nvarchar(max))
insert into @Customer values('this is a statement'), ('statement is this'), ('this')


select case when charindex('statement',comments) <> 0 then REPLACE(comments,substring(comments, charindex('statement',comments), LEN('statement')),'')
else comments end from @Customer 

select * from @Customer
Sign up to request clarification or add additional context in comments.

Comments

1

You should probably change that to use > greater than comparison

where charindex('statement',comments) > 0

(OR) Use like operator

where comments like '%statement%'

2 Comments

Yes I tried both of them but it still updates records to ' ' which do not contain string 'Statement' in it
@DipenShah, post sample data where statement not present and it does the update still.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.