この解決策を試してください:
SET NOCOUNT ON;
DECLARE @MyTable TABLE
(
ID INT IDENTITY(1,1) PRIMARY KEY,
OldContent NVARCHAR(MAX) NOT NULL,
NewContent NVARCHAR(MAX) NULL
);
INSERT INTO @MyTable (OldContent)
VALUES (N'<p>Please refer to <|Specific Policy Name<| policy.</p>');
WITH UpdateCTE
AS
(
SELECT b.NewContent,STUFF(b.InnerText,b.StartIndex-5,b.EndIndex-b.StartIndex+10,'<a href="'+b.[Text]+'">'+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
FROM @MyTable t
) a
) b
)
UPDATE UpdateCTE
SET NewContent = ChangedText;
SELECT *
FROM @MyTable x;
結果:
ID OldContent NewContent
--- ------------------------------------------------------------- ------------------------------------------------------------------------------------
1 <p>Please refer to <|Specific Policy Name<| policy.</p> <p>Please refer to <a href="Specific Policy Name">Specific Policy Name</a> policy.</p>