2016-12-02 58 views
0

過濾條件我想有字符/字符串數組省略如下的條件,在存儲過程SQL Server 2008中的存儲過程腳本

if(charindex(@strfileName, ' ')) <=0 and if(charindex(@strfileName, '-') <= 0) and 
if(charindex(@strfileName, '-') <= 0) and if(substring(@strfileName, 'report ') <= 0) 

什麼是做到這一點的最好辦法,通過陣列/過濾條件列表?

感謝,

+0

CHARINDEX將不會返回一個負數。你到底想做什麼? –

回答

1

您應該使用CHARINDEX輸入參數以相反的順序

請檢查下面的SQL腳本返回的值,如果它不包含任何禁止清單(空間中,「 - 」 '' 和 '報告')

declare @strfileName nvarchar(max) = N'kodyazcom' 

select @strfileName 
where 
charindex(' ', @strfileName) <= 0 and 
charindex('-', @strfileName) <= 0 and 
charindex('.', @strfileName) <= 0 and 
charindex('report ', @strfileName) <= 0 
2

對於 「字符數組」 你可以使用LIKE

IF @strfileName NOT LIKE '%[ -!:?]%' 
AND @strfileName NOT LIKE '%report%' 

PATINDEX其工作原理類似CHARINDEX但是支持模式:

DECLARE @strfileName VARCHAR(100) = 'asdf!xxx' 

IF PATINDEX('%[ -!:?]%', @strfileName) > 0 
    PRINT 'bad name' 
ELSE 
    PRINT 'good name' 
GO 
0

您可以用表和測試使用存在(選擇工作...

Declare @tableVerify table (charTest char, strTest varchar(100)) 
declare @strfileName varchar(100) 
set @strfileName = 'file-01.doc' 

insert into @tableVerify values (' ', 'report') 
insert into @tableVerify values ('-', 'data') 

if exists(select * from @tableVerify where not CHARINDEX(charTest, @strfileName) <= 0) 
begin 
    Select '2' 
end 
else 
begin 
    Select 'e' 
end