试试这个——
询问:
DECLARE @temp TABLE
(
id INT IDENTITY(1,1)
, [description] NVARCHAR(MAX)
)
INSERT INTO @temp ([description])
VALUES ('
<div class="text"> some data <!--test</div>
<ul>
<li>test</li>
<li>test2</li> <!-- Some comment
</ul>')
;WITH cte AS
(
SELECT t.id, t.token
FROM (
SELECT
t.id
, token =
SUBSTRING(
t.[description]
, number
, ABS(CHARINDEX('<', t.[description], number + 1) - number))
FROM @temp t
CROSS JOIN [master].dbo.spt_values n
WHERE [type] = 'p'
AND number <= LEN(t.[description]) - 1
AND SUBSTRING(t.[description], number, 1) = '<'
) t
WHERE t.token NOT LIKE '<!--%'
)
UPDATE t
SET [description] = (
SELECT c.token
FROM cte c
WHERE c.id = t.id
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
FROM @temp t
SELECT *
FROM @temp
结果:
<div class="text"> some data </div>
<ul>
<li>test</li>
<li>test2</li>
</ul>