Solved

Filter Vulgar words and detect only 160 words in Stored Procedure

Posted on 2012-04-05
7
310 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:ewangoya
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:ewangoya
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:ewangoya
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

18 Experts available now in Live!

Get 1:1 Help Now