Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The following line works fine:

SQL
select * from newsdetails
where dbo.udf_StripHTML(NewsDescription) like '%fiber%'



But I want to do something like this:

SQL
select  * from newsdetails
where contains(dbo.udf_StripHTML(NewsDescription), '"fiber"')



My function:

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
Posted
Comments
VC.J 17-Sep-15 4:07am    
what error you are getting
Member 10441509 17-Sep-15 4:48am    
Incorrect syntax near '('.
[no name] 17-Sep-15 4:10am    
On a first look to the Syntax Definition of CONTAINS, the first argument of CONTAINS needs to be a fieldname. So maybe it is possible to include "dbo.udf_StripHTML(NewsDescription)" in the SELECT field list with an alias Name and use it later in "WHERE CONTAINS(...."
Member 10441509 17-Sep-15 4:51am    
select top 10 dbo.udf_StripHTML(NewsDescription) as mydesc ,* from newsdetails where contains(mydesc, '"fiber"')

but gives error

Invalid column name 'mydesc'.
[no name] 17-Sep-15 4:56am    
So it seems it is not possible...but maybe an SQL prof will give you a solution.

What is the reason to replace the first statement you Show which is working?

A full-text index is based on the contents of your table. The value returned from your UDF is not part of the contents of your table, and cannot be used in a full-text query.

Create a persisted computed column on your table, and add that column to your full-text index. Then run your CONTAINS query against that column.
SQL
ALTER TABLE newsdetails
ADD NewsDescriptionText As dbo.udf_StripHTML(NewsDescription) PERSISTED
GO

ALTER FULLTEXT INDEX ON newsdetails
ADD (NewsDescriptionText)
GO

SELECT *
FROM newsdetails
WHERE CONTAINS(NewsDescriptionText, '"fiber"')
GO
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900