Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-06
6
Medium Priority
?
581 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

972 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