我们有基于SQL Server的Wordpress安装,并希望从wp_post中的字段post_content中提取图像名称。我已经尝试了很多patindex,像条款等,但我不知道该怎么做。
这是post_content列中行内容的示例:
<a href="http://www.example.com/wp-content/uploads/2017/06/1-1.jpg"><img class="aligncenter size-large wp-image-133975" src="http://www.example.com/wp-content/uploads/2017/06/1-1-1024x682.jpg" alt="" width="604" height="402" /></a>
表格中有大约10 000条记录,post_content中的内容可以包含一个或多个图像。 我想提取像这样的图像名称:
/wp-content/uploads/2017/06/1-1.jpg
/wp-content/uploads/2017/06/1-1-1024x682.jpg
感谢您的帮助。
答案 0 :(得分:0)
如果您对TVF开放。
厌倦了提取字符串,我修改了一个解析函数来接受两个不相似的参数。
示例强>
Declare @YourTable table (ID int,post_content varchar(max))
Insert Into @YourTable values
(1,'<a href="http://www.example.com/wp-content/uploads/2017/06/1-1.jpg"><img class="aligncenter size-large wp-image-133975" src="http://www.example.com/wp-content/uploads/2017/06/1-1-1024x682.jpg" alt="" width="604" height="402" /></a>')
Select A.ID
,ImageSrc = B.RetVal
From @YourTable A
Cross Apply [dbo].[udf-Str-Extract](A.post_content,'.com','"') B
Where right(B.RetVal,4) in ('.jpg','.png','.gif')
<强>返回强>
ID ImageSrc
1 /wp-content/uploads/2017/06/1-1.jpg
1 /wp-content/uploads/2017/06/1-1-1024x682.jpg
感兴趣的UDF
CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table
As
Return (
with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
cte3(N) As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)
Select RetSeq = Row_Number() over (Order By N)
,RetPos = N
,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1)
From (
Select *,RetVal = Substring(@String, N, L)
From cte4
) A
Where charindex(@Delimiter2,RetVal)>1
)
/*
Max Length of String 1MM characters
Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/
答案 1 :(得分:0)
试试这个:
DECLARE @table TABLE (ID INT IDENTITY(1,1),data NVARCHAR(500))
INSERT @table VALUES('<a href="http://www.example.com/wp-content/uploads/2017/06/1-1.jpg"><img "D:/abc.jpg" class="aligncenter size-large wp-image-133975" src="http://www.example.com/wp-content/uploads/2017/06/1-1-1024x682.jpg" alt="" width="604" height="402" /></a>')
DECLARE @images TABLE (ID INT,data NVARCHAR(500),passed INT)
DECLARE @collecter INT = 1
WHILE(@collecter=1)
BEGIN
INSERT INTO @images
SELECT ID, SUBSTRING(REVERSE(data),CHARINDEX('gpj.',REVERSE(data))+4,500),0 FROM @table
UPDATE @images SET data = REVERSE(SUBSTRING(data,1,CHARINDEX('/',data+'/')-1))+'.jpg' WHERE passed = 0
UPDATE A SET A.data = SUBSTRING(A.data,1,CHARINDEX(B.data,A.data)-1) FROM @table A JOIN @images B ON A.ID = B.ID WHERE passed = 0
UPDATE @images SET passed = 1
IF EXISTS (SELECT 1 FROM @table GROUP BY data HAVING CHARINDEX('.jpg',data)>0) SET @collecter = 1 ELSE SET @collecter = 0
END
SELECT data FROM @images