另一种答案是,在进行搜捕之前,使用CTE来排除超文本。
下面的CTE摘录可能符合搜索标准的行文,可追溯性地排除了超文本。 然后,Query利用CTE的结果,过滤仍然含有超文本的浏览器和没有完全达到搜索标准的浏览器。
CTE是很复杂的。 大部分赛事是处理PATINDEX返回。
--** Test table
DECLARE @HTML TABLE (id INT IDENTITY, html VARCHAR(max))
INSERT INTO @HTML SELECT This is a <span style="font-weight: bold; ">nice</span> question ;
INSERT INTO @HTML SELECT The cat sat <span style="font-weight: bold; ">on the</span> mat ;
--** Search criteria
DECLARE @Search VARCHAR(50) = is a nice ;
--** CTE to return the matching rows ignoring the HTML
;WITH Search_CTE (html_id, html_text)
AS (
SELECT h.id AS html_id
, LEFT(h.html,REPLACE(PATINDEX( %<% ,h.html)-1,-1,999999)) + SUBSTRING(h.html,CONVERT(INT,REPLACE(PATINDEX( %>% ,h.html)+1,1,999999)),LEN(h.html)) AS html_text
FROM @HTML AS h
WHERE h.html LIKE % + REPLACE(@Search, , % ) + %
UNION ALL
SELECT c.html_id AS html_id
, LEFT(c.html_text,REPLACE(PATINDEX( %<% ,c.html_text)-1,-1,999999)) + SUBSTRING(c.html_text,CONVERT(INT,REPLACE(PATINDEX( %>% ,c.html_text)+1,1,999999)),LEN(c.html_text)) AS html_text
FROM Search_CTE AS c
WHERE PATINDEX( %<% ,c.html_text) > 0
)
SELECT h.html AS Original HTML
, cte.html_text AS HTML Text
FROM Search_CTE AS cte
JOIN @HTML AS h
ON h.id = cte.html_id
WHERE PATINDEX( %<% ,cte.html_text) = 0 --** Filter out rows still containing HTML
AND html_text LIKE % + @Search + % ; --** Filter out rows not matching the search criteria
这种质疑有限制,即它处理“......”或“......”的情况;在案文中,如果需要,可加以编码。