Solved

SQL Serer - Strip out HTML code from varchar field...

Posted on 2013-11-06
6
503 Views
Last Modified: 2013-11-06
I have an old database that I am migrating data from.
One of the fields is a varchar with text like this in it:-

<p><span style="font-size: 9.5pt">A String Quartet with a twist! Stringfever are a musical group who combine the traditional and the futuristic; the unquestionable style of the classically trained fused with the funky and modern. Their abilities range from classical string skills to beat-boxing and plenty in between.</span></p>  <div style="line-height: normal; margin: 0cm 0cm 10pt"><span style="font-size: 9.5pt">Since 2004, Stringfever have performed their after dinner show at over 700 major corporate events throughout Europe, including&nbsp;Gala dinners, Award Ceremonies, Christmas Parties and&nbsp;Conferences. Their show usually lasts around 30 minutes and is equally suited as a grand finale to an evening or for lifting the mood of an audience before an award ceremony or presentation.</span></div>  <div style="line-height: normal; margin: 0cm 0cm 10pt"><span style="font-size: 9.5pt">Stringfever is made up of:</span></div>  <div style="line-height: normal; margin: 0cm 0cm 10pt"><b><span style="font-size: 9.5pt">Giles Broadbent</span></b><span style="font-size: 9.5pt"><br />  Leader of the pack. Former West End konzertmeister. Royal Academy of Music, gypsy violin demon, you name it, he'll play it - the faster the better. Favourite sports: skiing, cordless bungee jumping. Plays a bright blue 5 string Violectra.</span></div>  <div style="line-height: normal; margin: 0cm 0cm 10pt"><b><span style="font-size: 9.5pt">Ralph Broadbent</span></b><span style="font-size: 9.5pt"><br />  MC and chief musical arranger. Royal Academy of Music. Less height than his band mates but more strings. Football mad. Plays a 6 string Violectra. </span></div>  <div style="line-height: normal; margin: 0cm 0cm 10pt"><b><span style="font-size: 9.5pt"><div>

Open in new window


As you can see it is a mess - years of rich text editors inserting tags here there and everywhere!

Is there a trick way to remove any text held between <...> tag delimiters?

Regards,
0
Comment
Question by:splanton
  • 2
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
mikeyd234 earned 500 total points
ID: 39627312
Here's a function to do just that:
CREATE FUNCTION [dbo].[udf_StripHTML] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX) AS
BEGIN
    DECLARE @Start INT
    DECLARE @End INT
    DECLARE @Length INT
    SET @Start = CHARINDEX('<',@HTMLText)
    SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
    SET @Length = (@End - @Start) + 1
    WHILE @Start > 0 AND @End > 0 AND @Length > 0
    BEGIN
        SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
        SET @Start = CHARINDEX('<',@HTMLText)
        SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
        SET @Length = (@End - @Start) + 1
    END
    RETURN LTRIM(RTRIM(@HTMLText))
END
GO

-- how to use
select dbo.udf_StripHTML(myTable.mycolumnThatContainsHTML) 

Open in new window

0
 
LVL 22

Expert Comment

by:plusone3055
ID: 39627313
Splanton

the following link
http://sqlserverplanet.com/tsql/substring-between-two-characters

will show an example as how to elimiante characters before, after, and inbetween chracters
0
 
LVL 3

Expert Comment

by:mikeyd234
ID: 39627321
Tut tut +1 beating me again, though I have provided a nice function to strip html :D
0
 
LVL 2

Author Closing Comment

by:splanton
ID: 39627446
Mikey,
I knew some clever soul would have got a function for this. Glad I didn't waste hours re-inventing the wheel.
Thank you for such a prompt, concise and clear answer.

Regards,
0
 
LVL 2

Author Comment

by:splanton
ID: 39627477
Oooops! Sorry guys, looks like I was allocating point whilst further answers came in.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now