User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 402,028 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,456 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.

Checking for banned words

Join Date: May 2007
Posts: 8
Reputation: Zadj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Zadj Zadj is offline Offline
Newbie Poster

Question Checking for banned words

  #1  
May 16th, 2007
Hi,

I've run into a situation where I can not see how to write efficient code to perform a check that I need to do. I am running on MSSQL 2005

The check is to compare a varchar passed in to the stored procedure against the words in a table. The catch is that it is not the full word that needs to be checked but any part of the word. This is to stop the restricted words appearing in the text passed in. (eg the banned word test in the text 'this is a testing sentence'). I have got code that works but is very inefficient as it works through each word in the banned words and compares these against the supplied word/sentence.

Any advice on how I could do this more efficiently would be greatly appreciated.

Thanks, Zadj

ps. Here is the code that I am currently using:
  1. DECLARE @Result INT
  2. DECLARE @Testword VARCHAR(50)
  3. SET @Testword = 'Testword'
  4.  
  5. CREATE TABLE #RestrictedWords (
  6. Word VARCHAR(50)
  7. )
  8.  
  9. --Populate temp table from the settings table
  10. /*INSERT INTO #RestrictedWords
  11. SELECT * FROM SettingsRestrictedWords*/
  12. --Use test entries just now
  13. INSERT INTO #RestrictedWords
  14. (Word)
  15. VALUES ('test')
  16. INSERT INTO #RestrictedWords
  17. (Word)
  18. VALUES ('dog')
  19.  
  20. --Get first restricted word
  21. DECLARE @CurBadWord VARCHAR(50)
  22. SET @CurBadWord = (SELECT TOP 1 Word FROM #RestrictedWords)
  23.  
  24. WHILE (@CurBadWord IS NOT NULL)
  25. BEGIN
  26. -- Test the word and return the error code if match
  27. IF (@Testword LIKE '%' + @CurBadWord + '%')
  28. SET @Result = -2
  29.  
  30. -- Move to the next word
  31. DELETE FROM #RestrictedWords WHERE Word = @CurBadWord
  32. SET @CurBadWord = (SELECT TOP 1 Word FROM #RestrictedWords)
  33. END
  34. --Clean up
  35. DROP TABLE #RestrictedWords
  36.  
  37. SELECT @Result -- Return the result
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 10:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC