You must Sign In to post a response.
  • Category: SQL Server

    How to remove HTML tag from a string in sql server

    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 i get a output.

    but i have another html tags.

    how to execute that another html tags at single select query and get two html tag output.
  • #769472
    Hi,

    I run your functions it is removing all the HTML tags.Please let me know what other HTML tags are you referring to?

    Thanks!
    Anjali

    Thanks!
    Anjali Bansal

    ~Give your best and lead the world


  • Sign In to post your comments