2014-03-26 37 views
2

我的表中有一个数据列,并且在此列中可以有零,一个或多个沿着每行其他文本的URL。我想将这些网址提取到仅包含这些网址的新数据集中。我如何从文本(NVARCHAR(MAX))列中提取一个或多个URL

为什么?因为我想将这些URL中的一些添加到我的数据库中的阻止列表中以防止垃圾邮件。

例如,我有这样的文字数据列:

httx://portugal-forex.com/ 
httx://phen375treatment.com/ 
httx://priligy2000.org/ 
And so on. 

我真的不知道从哪里开始在这样做:

hmaruqbtufcvdlfu, <a href="httx://portugal-forex.com/">Day forex signal strategy trading</a>, KzxiIIO, [url=httx://portugal-forex.com/]Forex Broker[/url], mtNZQDi, httx://portugal-forex.com/ The best forex broker, IBWlBzg, <a href="httx://phen375treatment.com/">Avantage inconveniant phen 375</a>, ApEuXTp, [url=httx://phen375treatment.com/]Phen375[/url], QDVLpSn, httx://phen375treatment.com/ Where to buy phen 375, Fnwpugj, <a href="httx://priligy2000.org/">Priligy t</a>, zwRZhIC, [url=httx://priligy2000.org/]Order priligy[/url], FBgSaWs, httx://priligy2000.org/ Priligy buy online, FsemWnW, <a href="httx://ossorio.org/">Online Casino</a>, aOBtTaK, [url=httx://ossorio.org/]Online Casino[/url], oMMMacf, httx://ossorio.org/ Free online casino bounuses, occFLyZ, <a href="httx://paroxetine247.com/">Paroxetine adema</a>, xvrIdnq, [url=httx://paroxetine247.com/]Paroxetine depression[/url], MLSRAXX, httx://paroxetine247.com/ Paroxetine dark skin, GLYTcZY, <a href="httx://resolvedisputes.org/">Fioricet prescription online</a>, PmEMaMA, [url=httx://resolvedisputes.org/]Fioricet wcodiene for headache[/url], vPlKLhq, httx://resolvedisputes.org/ Online pharmacy fioricet, fxfhRcV. 

然后我想在文本中的所有网址SQL。

+0

需要得到唯一的主域名像httx://portugal-forex.com/或者也可以是httx://portugal-forex.com/xxx?Page = 2 – Darka

+0

主域名足够了 –

回答

2

这里是一个例子。我从“httx://”搜索字符串到第一个“/”:

在任何情况下,您都需要逐行阅读。

放置代码到功能

CREATE FUNCTION Temporary.getLinksFromText (@Tekstas NVARCHAR(MAX)) 
RETURNS @Data TABLE(TheLink NVARCHAR(500)) 
AS 
BEGIN 

    DECLARE @FirstIndexOfChar INT, 
      @LastIndexOfChar INT, 
      @LengthOfStringBetweenChars INT , 
      @String VARCHAR(MAX) 

    SET @FirstIndexOfChar = CHARINDEX('httx://',@Tekstas,0) 

    WHILE @FirstIndexOfChar > 0 
    BEGIN 

     SET @String = '' 
     SET @LastIndexOfChar = CHARINDEX('/',@Tekstas,@FirstIndexOfChar+7) 
     SET @LengthOfStringBetweenChars = @LastIndexOfChar - @FirstIndexOfChar + 1 

     SET @String = SUBSTRING(@Tekstas,@FirstIndexOfChar,@LengthOfStringBetweenChars) 
     INSERT INTO @Data (TheLink) VALUES (@String); 

     SET @Tekstas = SUBSTRING(@Tekstas, @LastIndexOfChar, LEN(@Tekstas)) 
     SET @FirstIndexOfChar = CHARINDEX('httx://',@Tekstas, 0) 

    END 

    RETURN 
END 

创建一些测试数据:

CREATE TABLE #Data(weLink NVARCHAR(MAX)); 
INSERT INTO #Data VALUES 
('hmaruqbtufcvdlfu, <a href="httx://portugal-forex.com/">Day forex signal strategy trading</a>, KzxiIIO, [url=httx://portugal-forex.com/]Forex Broker[/url], mtNZQDi, httx://portugal-forex.com/ The best forex broker, IBWlBzg, <a href="httx://phen375treatment.com/">Avantage inconveniant phen 375</a>, ApEuXTp, [url=httx://phen375treatment.com/]Phen375[/url], QDVLpSn, httx://phen375treatment.com/ Where to buy phen 375, Fnwpugj, <a href="httx://priligy2000.org/">Priligy t</a>, zwRZhIC, [url=httx://priligy2000.org/]Order priligy[/url], FBgSaWs, httx://priligy2000.org/ Priligy buy online, FsemWnW, <a href="httx://ossorio.org/">Online Casino</a>, aOBtTaK, [url=httx://ossorio.org/]Online Casino[/url], oMMMacf, httx://ossorio.org/ Free online casino bounuses, occFLyZ, <a href="httx://paroxetine247.com/">Paroxetine adema</a>, xvrIdnq, [url=httx://paroxetine247.com/]Paroxetine depression[/url], MLSRAXX, httx://paroxetine247.com/ Paroxetine dark skin, GLYTcZY, <a href="httx://resolvedisputes.org/">Fioricet prescription online</a>, PmEMaMA, [url=httx://resolvedisputes.org/]Fioricet wcodiene for headache[/url], vPlKLhq, httx://resolvedisputes.org/ Online pharmacy fioricet, fxfhRcV.'), 
('hmaruqbtufcvdlfu, <a href="httx://portugal-forex.com/">Day forex signal strategy trading</a>, KzxiIIO, [url=httx://portugal-forex.com/]Forex Broker[/url], mtNZQDi, httx://portugal-forex.com/ The best forex broker, IBWlBzg, <a href="httx://phen375treatment.com/">Avantage inconveniant phen 375</a>, ApEuXTp, [url=httx://phen375treatment.com/]Phen375[/url], QDVLpSn, httx://phen375treatment.com/ Where to buy phen 375, Fnwpugj, <a href="httx://priligy2000.org/">Priligy t</a>, zwRZhIC, [url=httx://priligy2000.org/]Order priligy[/url], FBgSaWs, httx://priligy2000.org/ Priligy buy online, FsemWnW, <a href="httx://ossorio.org/">Online Casino</a>, aOBtTaK, [url=httx://ossorio.org/]Online Casino[/url], oMMMacf, httx://ossorio.org/ Free online casino bounuses, occFLyZ, <a href="httx://paroxetine247.com/">Paroxetine adema</a>, xvrIdnq, [url=httx://paroxetine247.com/]Paroxetine depression[/url], MLSRAXX, httx://paroxetine247.com/ Paroxetine dark skin, GLYTcZY, <a href="httx://resolvedisputes.org/">Fioricet prescription online</a>, PmEMaMA, [url=httx://resolvedisputes.org/]Fioricet wcodiene for headache[/url], vPlKLhq, httx://resolvedisputes.org/ Online pharmacy fioricet, fxfhRcV.') 

你可以这样执行它(无光标)

SELECT allLinks.* 
FROM #Data AS D 
OUTER APPLY Temporary.getLinksFromText (D.weLink) AS allLinks 
+0

此解决方案的工作原理,但我必须创建一个cusor来处理所有不是最优的行。没有光标可以做到吗? =)Setbased代替。我想处理这个语句“SELECT Data FROM Paste WHERE paste.CaptchaOK = 0”。 –

+0

你的意思是paste.CaptchaOK = 0? – Darka

+0

我认为你无论如何都需要一个一个去。检查更新的答案。 – Darka