narasiman rao

narasiman rao

  • NA
  • 519
  • 745.6k

How to Remove HTML Tags from String in SQL Server

Dec 12 2017 7:05 AM
i have created one function as follows
 
CREATE FUNCTION [dbo].[fn_parsehtml]
(
@htmldesc varchar(max)
)
returns varchar(max)
as
begin
declare @first int, @last int,@len int
set @first = CHARINDEX('<',@htmldesc)
set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc))
set @len = (@last - @first) + 1
while @first > 0 AND @last > 0 AND @len > 0
begin
---Stuff function is used to insert string at given position and delete number of characters specified from original string
set @htmldesc = STUFF(@htmldesc,@first,@len,'')
SET @first = CHARINDEX('<',@htmldesc)
set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc))
set @len = (@last - @first) + 1
end
return LTRIM(RTRIM(@htmldesc))
end
select dbo.fn_parsehtml('<table WIDTH="100%"CELLPADDING="0"CELLSPACING="1"><tr style="COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 10pt;BACKGROUND-COLOR: #f2f2f2;"><td><b>MRD No:</b> 6777 </td><td><b>InPatient Number: </b> IP/150402/00009 </td><td><b>Patient Name: </b> MR. BALASUBRAMANIAN K </td><td><b>Gender: </b> Male </td><td><b>Age: </b> 69 Year(s) </td></tr><tr style="COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 10pt;BACKGROUND-COLOR: #f2f2f2;"><td colspan = 5 ><b>Consultant: </b> Dr.SUDHIR R R </td></tr></table>
<b>Regional Anaesthesia</b></a><br><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0"><TR /><TR /><TR tabindex="1" style="COLOR: black; FONT-FAMILY: Arial Black;FONT-SIZE: 8pt; BACKGROUND-COLOR: #e5f4fb"><TD colspan="6" Default="--" align="center"><B>REGIONAL ANAESTHESIA</B></TD></TR><TR tabindex="1" style="COLOR: black; FONT-FAMILY: Arial;FONT-SIZE: 8pt; BACKGROUND-COLOR: #f2f2f2"><TD Default="--" width="10%"><B>Weight</B></TD><TD DefaultElement="" width="10%"> </TD><TD Default="--" width="10%"><B>kg</B></TD><TD Default="--" width="10%"><B>ASA grade</B></TD><TD DefaultElement="" width="10%" style="background-color:#F2CEFB">II </TD><TD DefaultElement="" width="10%"> </TD></TR></TABLE><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0"><TR /><TR /><TR tabindex="1" style="COLOR: black; FONT-FAMILY: Arial;FONT-SIZE: 8pt; BACKGROUND-COLOR: #f2f2f2"><TD Default="--" width="15%"><B>Regional Anaesthesia</B></TD><TD DefaultElement="" width="15%" style="background-color:#F2CEFB">Satisfactory </TD><TD Default="--" width="15%"><B>Technique</B></TD><TD DefaultElement="" width="15%" style="background-color:#F2CEFB">Peribulbar </TD><TD Default="--" width="15%"><B>Drugs</B></TD><TD DefaultElement="" width="15%"> </TD></TR></TABLE><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0"><TR /><TR /><TR tabindex="1" style="COLOR: black; FONT-FAMILY: Arial;FONT-SIZE: 8pt; BACKGROUND-COLOR: #f2f2f2"><TD Default="--" width="15%"><B>Xylocaine</B></TD><TD DefaultElement="" width="15%" style="background-color:#F2CEFB">2 </TD><TD Default="--" width="15%"><B>%</B></TD><TD DefaultElement="" width="15%" style="background-color:#F2CEFB">4 </TD><TD Default="--" width="15%"><B>ml(...mgs)</B></TD></TR><TR tabindex="1" style="COLOR: black; FONT-FAMILY: Arial;FONT-SIZE: 8pt; BACKGROUND-COLOR: #f2f2f2"><TD Default="--" width="15%"><B>Marcaine</B></TD><TD DefaultElement="" width="15%" style="background-color:#F2CEFB">0.5 </TD><TD Default="--" width="15%"><B>%</B></TD><TD DefaultElement="" width="15%" style="background-color:#F2CEFB">4 </TD><TD Default="--" width="15%"><B>ml(...mgs)</B></TD></TR></TABLE>
 
once i execute the above query output i get no problem
 
but like this i have multiple html tags.
 
how to execute multiple html tags at single select query.

Answers (5)