当前位置: 代码迷 >> Sql Server >> 转义替换有关问题
  详细解决方案

转义替换有关问题

热度:41   发布时间:2016-04-24 09:30:36.0
转义替换问题
如题,此html代码应如何替换
<div class="cont_explain">%</div>

原意是
UPDATE ecs_goods SET goods_desc=REPLACE(goods_desc,'<div class="cont_explain">%</div>','abc')


就是想把<div class="cont_explain">到</div>之间的代码替换掉


可能还需要where语句,可能replace不支持%,这种情况应该怎么写呢... 
------解决思路----------------------
你自己把下面的代码改成函数。
--参数
DECLARE @html varchar(max)
DECLARE @startTag varchar(max)
DECLARE @endTag varchar(max)
DECLARE @replace varchar(max)

SET @html = '<div class="cont_explain">aaa</div>
<div class="cont">bbb</div>
<div class="cont_explain">ccc</div>'
SET @startTag = '<div class="cont_explain">'
SET @endTag = '</div>'
SET @replace = 'abc'

-- 内部
DECLARE @i1 int, @i2 int

SET @i1 = CHARINDEX(@startTag, @html)
WHILE @i1<>0
BEGIN
    SET @i2 = CHARINDEX(@endTag, @html, @i1+LEN(@startTag))
    IF @i2 = 0
    BEGIN
        SET @i1 = 0
    END
    ELSE
    BEGIN
        SET @html = STUFF(@html, @i1, @i2-@i1+LEN(@endTag), @replace)
        --PRINT @html
        SET @i1 = CHARINDEX(@startTag, @html, @i1+LEN(@replace))
    END
END
--RETURN
PRINT @html

abc
<div class="cont">bbb</div>
abc
  相关解决方案