替换字段中的HTML标签标签内的属性,保留标签对包含的内容

---原版

CREATE FUNCTION ReplaceHTML
(
 @source nvarchar(max) --原字符串
)
RETURNS nvarchar(max)
AS
BEGIN
 declare @html nvarchar(max)
 set @html = @source
 while CHARINDEX('<',@html)>0
 set @html=STUFF(@html,charindex('<',@html),charindex('>',@html)-charindex('<',@html)+1,'');
 return @html;
END
GO



----修改后

create FUNCTION ReplaceHTML
(
 @source nvarchar(max) --原字符串
)
RETURNS nvarchar(max)
AS
BEGIN
 declare @html nvarchar(max)
 set @html = @source
 while CHARINDEX('<',@html)>0
 ----替换标签
 set @html=STUFF(@html,charindex('<',@html),charindex('>',@html)-charindex('<',@html)+1,'');
 ----替换特殊转移符
 set @html=REPLACE(@html,'&nbsp;','')
 return @html;
END
GO


注意:调用时有可能会遇到如下提示:'ReplaceHTML' 不是可以识别的 内置函数名称
这时需要在 函数名前+dbo.,即:dbo.ReplaceHTML('aaaa')


本文转载:CSDN博客