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 391,142 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 3,186 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.
Please support our MS SQL advertiser:
Views: 1035 | Replies: 2 | Solved
Reply
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  
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 834
Reputation: ithelp is on a distinguished road 
Rep Power: 4
Solved Threads: 61
ithelp ithelp is offline Offline
Practically a Posting Shark

Re: Checking for banned words

  #2  
May 16th, 2007
May be you can add the possible combinations in database
Reply With Quote  
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

Solution Re: Checking for banned words

  #3  
May 16th, 2007
Thanks for the reply.

I've been given the answer on a different forum. The tip is CHARINDEX

In case anyone is interested here is the resulting code
  1. DECLARE @Result INT
  2. DECLARE @Testword VARCHAR(50)
  3. SET @Testword = 'Tesword'
  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. /* Here is the changed code */
  21. SELECT @Result=-2 FROM #RestrictedWords
  22. WHERE CHARINDEX(Word, @TestWord) > 0
  23. /***********************/
  24.  
  25. --Clean up
  26. DROP TABLE #RestrictedWords
  27.  
  28. SELECT @Result -- Return the result
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 6:47 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC