Solved

SQL Server 2000 - Selecting From A Field That Contains HTML

Posted on 2007-11-20
6
201 Views
Last Modified: 2010-03-20
Hello,
I have got a field in my database that has HTML code. I need to pass a parameter of a keyword to my SP in order to find this keyword in my field "LIKE '%' + @Keyword + '%". Is there a way to ignore those HTML tags while searching?
0
Comment
Question by:feesu
6 Comments
 
LVL 31

Accepted Solution

by:
James Murrell earned 50 total points
ID: 20318608
0
 

Assisted Solution

by:BadKarma
BadKarma earned 25 total points
ID: 20318609
You could create a procedure that strips HTML from results, which would also be useful for other things.

One I use is:

USE [mediadienstencentrumdb1]
GO
/****** Object:  UserDefinedFunction [dbo].[RemoveHTML]    Script Date: 11/20/2007 11:11:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE function [dbo].[RemoveHTML] (@intro varchar(4000))
RETURNS varchar(4000)
AS
BEGIN
            declare @leftbracket char(1)
            declare @rightbracket char(1)
            declare @resultstring varchar(4000)
            declare @remainingString varchar(4000)
            declare @openbracketposition int
            declare @closebracketposition int
            set @leftbracket = '<'
            set @rightbracket = '>'
            set @resultstring = ''
            set @remainingString = @intro
            while len(@remainingString) > 0
            begin
                        set @openbracketposition = charindex(@leftbracket, @remainingString)
                        if @openbracketposition > 0
                        begin
                                    set @resultstring = @resultstring + left(@remainingString, @openbracketposition - 1)
                                    set @remainingString = substring(@remainingString, @openbracketposition, 3000)
                                    set @closebracketposition = charindex(@rightbracket, @remainingString)
                                    if @closebracketposition > 0
                                                set @remainingString = substring(@remainingString, @closebracketposition + 1, 3000)
                                    else
                                                set @remainingString = ''
                        end
                        else
                        begin
                                    set @resultstring = @resultstring + @remainingString
                        	    set @remainingString = ''
                        end
            end
RETURN @resultstring
END

Open in new window

0
 

Expert Comment

by:BadKarma
ID: 20318615
Oh, and ignore that first USE. It's one db where I use the function.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 50 total points
ID: 20333091
go to the first comment's link - accepted solution has a much better UDF.

dbo.ufn_removetags(fieldname) LIKE '%' + @Keyword + '%"
0
 

Author Comment

by:feesu
ID: 20402255
I tried creating the function from the first link, but got the following errors:

drop function dbo.UDF_RemoveTags
do
Create function UDF_RemoveTags(@inStr varchar(8000)) returns varchar(8000) as
begin
declare @pos int
set @pos = CHARINDEX('<', @inStr) * sign(charindex('>', @instr))
WHILE @pos > 0
Select @inStr = STUFF(@inStr, @pos, CHARINDEX('>', @inStr) - @pos + 1, ' ') ,@pos = CHARINDEX('<', @inStr) * sign(charindex('>', @instr))
return @inStr
end
go



Error:

Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'do'.
Server: Msg 111, Level 15, State 1, Line 3
'CREATE FUNCTION' must be the first statement in a query batch.
Server: Msg 137, Level 15, State 1, Line 6
Must declare the variable '@inStr'.
Server: Msg 137, Level 15, State 1, Line 8
Must declare the variable '@inStr'.
Server: Msg 137, Level 15, State 1, Line 9
Must declare the variable '@inStr'.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20402265
change the DO to GO
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

816 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

8 Experts available now in Live!

Get 1:1 Help Now