Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Filter Vulgar words and detect only 160 words in Stored Procedure

Posted on 2012-04-05
7
Medium Priority
?
343 Views
Last Modified: 2012-04-13
Trying to filter vulgar words and limit to 160 words in @Message variable before insert statement, and return several strings forout put,

@ExpireDate DATETIME,
 @ReturnString VARCHAR(100) OUTPUT
 
AS  

BEGIN 
     SET NOCOUNT ON 
     
     IF (CHARINDEX('bitch', @Message) > 0) 
     
		@ReturnString = 'Please do not use vulgar words'
     
     ELSE
     

     INSERT INTO dbo.fSMSBuffB
          ( 
            fBatchNo                     ,
            fOrderNo                     ,
            fRefNo                       ,
            fPriority                    ,
            fFromID                      ,
            fMobileNumber                ,
            fMessage					 ,
            fInputDate					 ,
            fSuccessDate				 ,
            fExpireDate        
          ) 
     VALUES 
          ( 
            @BatchNo                      ,
            @OrderNo                      ,
            @Refno                        ,
            @Priority                     ,
            @FromID                       ,
            @MobileNumber                 ,
            @Message                      ,
            @InputDate                    ,
            @SuccessDate                  ,
            @ExpireDate
          ) 

END 

Open in new window

0
Comment
Question by:doramail05
  • 3
  • 3
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 37809771
Best way would be to create a table that will hold the words you wish to filter. Then create a stored procedure that checks for the words.
something very simple like

create table InvalidWords(
	id int identity,
	sword varchar(32)
)
	
------
create procedure ValidateWords @Message varchar(8000), @status int out
as
begin
  declare @id int, @word varchar(32)
  set @status = 0
  set @id = 1
  
  while exists(select 1 from InvalidWords where id >= @id)
  begin
    select @id = ID, @word = sword from InvalidWords where id >= @id
    set @id = @id + 1 
    
    if CHARINDEX(@word, @Message, 1) >= 1
    BEGIN
		set @status = 1
		break
    END
  end
end

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 37809776
In your code, you could do

BEGIN
     SET NOCOUNT ON

    DECLARE @status int
     ValidateWords(@Message, @Status)
     
     IF @Status = 1
                @ReturnString = 'Please do not use vulgar words'
     ELSE
    .....................
0
 
LVL 1

Author Comment

by:doramail05
ID: 37809872
guess should be making a table to store and retrieve it,

But what if just without the method,

only by using this stored procedure to filter several words
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 37809943
That would become a little bit messy and hard to maintain, think adding a new word to your check.

You will end up with multiple conditions, as many as the words you would like to filter eg

if (CHARINDEX('xxx', @Message, 1)  >=1) OR (CHARINDEX('yyy', @Message, 1)  >=1) OR ..........
0
 
LVL 1

Accepted Solution

by:
doramail05 earned 0 total points
ID: 37822272
got it oredi

BEGIN 
     SET NOCOUNT ON 
    -- vulgar text filter start
		set @ReturnString=5
		DECLARE @S varchar(max),@Split char(1), @X xml
		SELECT @S = @Message, @Split = ' '
		SELECT @X = CONVERT(xml,'<root><s>' + REPLACE(@S,@Split,'</s><s>') + '</s></root>')
		DECLARE @vtext int
		--set @vtext=

		set @vtext=(select COUNT(*) from(
		SELECT [tValue] = T.c.value('.','varchar(20)'),vulgarword FROM @X.nodes('/root/s') T(c)
		left join tVulgarTable on T.c.value('.','varchar(20)') =tVulgarTable.vulgarword
		)
		t
		where t.vulgarword is not null)
		print @vtext
		
--Vulgar text filter end
    
     if (@vtext>0)
		begin
			set @ReturnString = 0
		end
	if (len(@Message)>160)
		begin
			set @ReturnString = 1
		end
	if (len(@Message)>160 and @vtext>0)
		begin
			set @ReturnString = 3
		end
	
	 if ((len(@Message)<160 or len(@Message)=160) and @ReturnString=5)
		begin
			
			set @ReturnString = 2
		
        
     INSERT INTO dbo.tableA

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37823312
assist from other
Then why are your not awarding points for assists?
0
 
LVL 1

Author Closing Comment

by:doramail05
ID: 37841666
assist from other
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

783 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