I have HTML in my database table which is of following Format
<img style='border-style: none' src='../Images/flag_Green.gif' onmouseover='ddrivetip("<table border=1 cellpadding=1 width=100%><tr><th nowrap width=20%>My Status</th><th>My Details</th></tr><tr><td>Green</td><td>Compliant - 06-0907370</td></tr></table>", 400, null, this);' onmouseout='hideddrivetip(this)'></img>
I need to extract colour value from this eg in this Case it's "green" Can anyone help me to write a function that can extract this? It's between first td tag
Pretty simple using SUBSTRING and PATINDEX as suggested by Tab Alleman. Here is one way you could do this.
declare @String varchar(1000) = '<img style=''border-style: none'' src=''../Images/flag_Green.gif'' onmouseover=''ddrivetip("<table border=1 cellpadding=1 width=100%><tr><th nowrap width=20%>My Status</th><th>My Details</th></tr><tr><td>Green</td><td>Compliant - 06-0907370</td></tr></table>", 400, null, this);'' onmouseout=''hideddrivetip(this)''></img>'
select SUBSTRING(@String, patindex('%<td>%', @String) + 4, patindex('%</td>%', @String) - patindex('%<td>%', @String) - 4)
I created function to extract text from html.
Create function [dbo].[RetriveTextFromHTML](@htmlstring varchar(Max))
returns varchar(Max)
AS
BEGIN
Set @htmlstring=Replace(@htmlstring,' ',' ');
Set @htmlstring=Replace(@htmlstring,'Note:','');
DECLARE @startTag varchar(25) = '%[<]%'
DECLARE @endTag varchar(25) = '%[>]%'
Declare @endTagIndex int =0;
Declare @startTagIndex int =0;
WHILE PATINDEX(@startTag,@htmlstring)>0
Begin
Set @startTagIndex=PATINDEX(@startTag,@htmlstring);
Set @endTagIndex=PATINDEX(@endTag,@htmlstring);
SET @htmlstring = Stuff(@htmlstring,@startTagIndex,(@endTagIndex-@startTagIndex)+1,'');
End
return RTRIM(@htmlstring)
END
Call this method as
Select [dbo].[RetriveTextFromHTML]('<strong>Hello World</strong>')
Output:
Hello World
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.