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
Archive
<June 2005>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
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: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)