从Sql Sqrver中的HTML列替换img src [英] Replace img src from a Html column in Sql Sqrver
本文介绍了从Sql Sqrver中的HTML列替换img src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以从Sql Sqrver中的HTML列替换img src吗?例如, SELECT TOP 1 html FROM TableName
会导致
Is there any way to replace img src from a Html column in Sql Sqrver? for example, SELECT TOP 1 html FROM TableName
result in
<p><img id="img" title="imgtitle" src="/source1.jpg" width="225" height="300" /> text</p>
我需要通过将/source1.jpg
替换为/source2.jpg
source1可以动态地是任何字符串(即首先我需要使用查询在html列中找到它).
source1 can be any string dynamically (i.e first I need to find it in the html column using query).
<p><img id="img" title="imgtitle" src="/source2.jpg" width="225" height="300" /> text</p>
我想到的一件事是使用正则表达式,但我不知道如何在SQL Server中使用正则表达式以及如何使用它进行更新.
There is one thing in my mind is using regex but I have no idea how to use regex in SQL server and how to update it using that.
有人知道怎么做吗?
推荐答案
您使用以下查询来实现您的目标:
You an use the following query to achieve your goal:
DECLARE @SRC VARCHAR(MAX) = '<P><IMG ID="IMG" TITLE="IMGTITLE" SRC="/SOURCE1.JPG" WIDTH="225" HEIGHT="300" /> TEXT</P>';
DECLARE @SUB VARCHAR(MAX) = RIGHT(@SRC, (LEN(@SRC)-PATINDEX ( '%SRC="%' , @SRC )-5));
DECLARE @LEN INT = CHARINDEX ( '"' , @SUB );
SELECT REPLACE(@SRC,SUBSTRING(@SRC,(PATINDEX ( '%SRC="%' , @SRC )+5),@LEN),'/NEWVALUE.JPG');
这篇关于从Sql Sqrver中的HTML列替换img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文