现在我正在运行查询以查找文档中的动态单词并将其替换为链接。它工作得很好,但是null
当有一些特殊字符时返回。目前我遇到动态词包含的问题ampersand '&'
以下是部分内容:
<p>STEPS:</p>
<p>
<p>Please refer to <|Cease & Desist|> policy.</p>
它正在返回一个NULL
for'Cease & Desist'
下面是我正在使用的代码,我需要的是它返回整个动态单词,即使它包含&符号。
SET NOCOUNT ON;
DECLARE @MyTable TABLE
(
ID INT IDENTITY(1,1) PRIMARY KEY,
PolicyName NVARCHAR(200) NULL,
DocumentID int not null,
OldContent NVARCHAR(MAX) NOT NULL,
NewContent NVARCHAR(MAX) NULL
);
INSERT INTO @MyTable (documentid,OldContent)
select documentid, html from IPACS_Document where PATINDEX('%Please refer to <|%', html) > 0 and PATINDEX('%|> Policy%', html) > 0 and documentid = 147;
--select html, PATINDEX('%Please refer to <|%', html), PATINDEX('%|> Policy%', html) from IPACS_Document where PATINDEX('%Please refer to <|%', html) > 0;
WITH UpdateCTE
AS
(
SELECT b.PolicyName, b.[text], b.NewContent,STUFF(b.InnerText,b.StartIndex-5,b.EndIndex-b.StartIndex+10,'<a href="~/Document/Details/'+ CAST(d.[documentid] as VARCHAR(200))+'">'+b.[Text]+'</a>') AS ChangedText
FROM
(
SELECT a.*,SUBSTRING(a.InnerText,a.StartIndex,a.EndIndex-a.StartIndex) AS [Text]
FROM
(
SELECT PATINDEX('%Please refer to <|%',t.OldContent)+21 AS StartIndex,
PATINDEX('%|> Policy%',t.OldContent) AS EndIndex,
t.OldContent AS InnerText,
t.NewContent,
t.PolicyName
FROM @MyTable t
) a
) b
inner join IPACS_Document d on d.filename like '%' + b.[text] + '%'
where d.categoryid = 3
)
--select * from UpdateCTE
UPDATE UpdateCTE
SET NewContent = ChangedText, PolicyName = [text];
SELECT *
FROM @MyTable x;
在上面的查询中,最内部的查询query 'a'
InnerText column name [text]
是包含动态词的内容。这被传递给table 'b'
as [text]
。最终在@Mytable
动态词中被放入PolicyName
。
当动态单词包含&符号或特殊字符时,如何允许它工作?