2016-11-11 44 views
1
select REPLACE(comments,substring(comments, 
charindex('statement',comments), LEN(comments)),'') 
from Customer 
where charindex('statement',comments) <>0 

我想更換隻能用替換功能從專欄的意見「的聲明」,但它取代了沒有「聲明」字符串它記錄爲「」替換功能。SQL使用CHARINDEX錯誤

回答

1

你或許應該改變,要使用>大於比較

where charindex('statement',comments) > 0 

(OR)使用like運算

where comments like '%statement%' 
+0

是的,我嘗試了他們兩個,但它仍然更新記錄到''哪些不包含字符串'語句'在它 –

+0

@DipenShah,張貼示例數據'語句'不存在,它仍然進行更新。 – Rahul

2

請嘗試這樣的,這隻會替換從評論字符串聲明:

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