Solved

Filter Vulgar words and detect only 160 words in Stored Procedure

Posted on 2012-04-05
7
313 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

911 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

24 Experts available now in Live!

Get 1:1 Help Now