?
Solved

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

Posted on 2013-11-06
6
Medium Priority
?
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
mikeyd234 earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

719 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