从HTML图像标签(img)

时间:2019-02-15 17:17:11

标签: sql sql-server tsql sql-server-2012

我正在尝试从包含HTML的数据库字段中提取图像路径。数据库中的数据如下所示:

<p><strong><strong>DISCUSSION POINT</strong>: Nearly how many years did it take Sir Francis Drake to complete the first circumnavigation of the globe in 1580?&nbsp;<br /><p><img id="lk45459gjh4" src="../mediaForExam/dlfkeiut8484034djjd222.png" alt="dlfkeiut8484034djjd222.png" width="697" height="352" /></p>

从这个字符串中,我只需要这一部分:

  

/mediaForExam/dlfkeiut8484034djjd222.png

我尝试了以下查询:

SELECT RIGHT(questionText, (LEN(questionText)-PATINDEX ( '%SRC="%' , questionText )-5)) AS MediaPath FROM exams.history

但是它返回的是这样的字符串:

  

./ mediaForExam / dlfkeiut8484034djjd222.png”   alt =“ dlfkeiut8484034djjd222.png” width =“ 697” height =“ 352” />

有没有办法只返回第一个斜杠,文件夹名称,然后返回文件名(即:/folderName/fileName.ext)

谢谢!

1 个答案:

答案 0 :(得分:1)

我会做的:

select substring(hh.questionText, 1, charindex('"', hh.questionText) - 1)  as MediaPath 
from exams.history h cross apply
     ( values (stuff(questionText, 1, charindex('src=', questionText)+6, '')) 
     ) hh (questionText);