[英]SQL Replace Url Wildcard
I need to match all records in sql and replace where urls match http://www.test.com/ etc. I need to remove from the beginning of the a tag to the closing tag. 我需要匹配sql中的所有记录,并替换url匹配http://www.test.com/的位置 。我需要从a标记的开头删除到结束标记。
For example content may have
same data here <a href="http://www.test.com/personal/car-leasing/kia/pro ceed/" rel="nofollow"
target="_blank"><img alt="Contract hire" src="/cms-images/car-hire.jpg" /></a> and here
I would need to remove 我需要删除
These links car vary but all start with http://www.test.com/ 这些链接的汽车各不相同,但都以http://www.test.com/开头
I tried with the below but am unsure how to escape in a replace statement 我尝试了以下内容,但不确定如何在replace语句中转义
SELECT content, REPLACE(content, '<a href="http://www.test.com/%','')
from cms.dbo.tblpost
where id = 47066
DECLARE @tblpost TABLE (
ID INT NOT NULL IDENTITY PRIMARY KEY
,content NVARCHAR(MAX)
)
INSERT INTO @tblpost (content)
VALUES
(N'example www.test.com/personal')
,(N'example content may have same data here <a href="http://www.test.com/personal/car-leasing/kia/pro ceed/" rel="nofollow"
target="_blank"><img alt="Contract hire" src="/cms-images/car-hire.jpg" /></a> and here')
DECLARE @Url NVARCHAR(4000) = N'www.test.com'
,@OpenTagRev NCHAR(2) = N'a<' -- Reversed open tag <a href
,@CloseTag NCHAR(4) = N'</a>'
;WITH SearchUrl as (
SELECT ID, content, CHARINDEX ( @Url ,content) UrlPos
FROM @tblpost
)
, SearchOpenCloseTag as (
SELECT ID, content, UrlPos, CHARINDEX(@OpenTagRev, REVERSE(LEFT(content, UrlPos))) OpenTagOffset, CHARINDEX ( @CloseTag ,content, UrlPos) CloseTagPos
FROM SearchUrl
WHERE UrlPos > 0
)
SELECT ID, content, STUFF(content, UrlPos - OpenTagOffset, CloseTagPos - UrlPos + OpenTagOffset + 4 /* 4 IS LEN(@CloseTag) */, N'') ReplContent
FROM SearchOpenCloseTag
WHERE OpenTagOffset > 0
AND CloseTagPos > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.