2012-07-29 69 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)+',%')