2012-07-29 70 views
0

我的列ServicetTypeIDs包含以下数据。我使用下面的where子句来搜索值,让我们说如果我的参数@ServiceTypes = 1,9它只会返回我的记录,当两个1,9都存在。我想返回包含1,9或记录有1,9本身的记录。我的where子句不正确。请帮助SQL在哪里CHARINDEX

column 
1 
NULL 
9 
1 
4,7 
1,9 

WHERE 
(@ServiceTypes is null or  
    charindex(','+SEP.ServiceTypeIDs+',',  
    ','[email protected]+',') > 0))) 

回答

0

如果你需要 '1,9' 选择( '1', '9', '1,9')这里是一个简短的条件

where (@ServiceTypes is null) or (','[email protected]+',' like '%,'+ServiceTypeIds+',%') 

但对于“4 ,9'只选择('9')。如果你想得到('4,7','1,9','9')这个掩码,这里是条件

where (@ServiceTypes is null) 
     or 
     (','+ServiceTypeIds+',' like ',%'+SUBSTRING(@ServiceTypes,1,charindex(',',@ServiceTypes)-1)+',%') 
     or 
     (','+ServiceTypeIds+',' like ',%'+SUBSTRING(@ServiceTypes,charindex(',',@ServiceTypes)+1,1000)+',%')