Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Thursday, June 23, 2005

This is an interesting piece of sql, the function accepts a string and removes HTML elements that is inside the string. This is genereally useful when you have data that has embedded HTML content on it. The function was written by Robert Davis.

/******
Object: User Defined Function dbo.fnStripTags
Script Author: Robert Davis, robertd@realtechllc.com
Purpose: Strip HTML tags out of text. Anything enclosed in '<' and '>' will be removed.

sample usage:
Declare @MyText varchar(30)
Set @MyText = '<b>My <i>sample</i> Text</b>'

Select dbo.fnStripTags(@MyText)

this returns: My sample Text

******/


Create Function dbo.fnStripTags
    (@Dirty varchar(4000))
    Returns varchar(4000)
As

Begin
    Declare @Start int,
        @End int,
        @Length int

    While CharIndex('<', @Dirty) > 0 And CharIndex('>', @Dirty, CharIndex('<', @Dirty)) > 0
     Begin
        Select @Start = CharIndex('<', @Dirty),
         @End = CharIndex('>', @Dirty, CharIndex('<', @Dirty))
        Select @Length = (@End - @Start) + 1
        If @Length > 0
         Begin
            Select @Dirty = Stuff(@Dirty, @Start, @Length, '')
         End
     End

    return @Dirty
End

Thursday, June 23, 2005 3:36:55 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
SQL
Comments are closed.
On this page
Archive
<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 246
This Year: 43
This Month: 4
This Week: 0
Comments: 111
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)