如何用sql查询替换给定字符串中的特定字符数?
答
第一@和第三@试试这个使用charindex()
和stuff()
:
declare @s1 varchar(100)= '[email protected]@[email protected]@', @count int = 0
select @count = charindex('@',@s1,@count+1),
@s1 = case isremove when 1 then
stuff(@s1, charindex('@',@s1,@count) ,1,'K') else @s1 end
from (values (1),(0),(1)) t(isremove)
declare @s1 varchar(100)= '[email protected]@[email protected]@'
select stuff(stuff(@s1,first,1,'K'),third,1,'K')
from (
select charindex('@',@s1,1) first,
charindex('@',@s1,charindex('@',@s1,charindex('@',@s1,1)+ 1)+1) third
)x
应该是需要更换第一和第三个字符。 – Sunny 2013-03-01 15:13:35
@孙德普,谢谢!固定 – Kaf 2013-03-01 15:17:49