1

I am making an rdl report and I have three check-boxes that if checked need to alter my WHERE statement. If none are checked the result should only match by date range. If one or more are checked it needs to return the fields that match the variables corresponding string.

WHERE 
(EffectiveDate BETWEEN @StartDate AND @EndDate) 
AND (((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR @IncludeSEWPrefix = 0)
AND ((@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR @IncludePAWPrefix = 0)
AND ((@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR @IncludeRPLPrefix = 0)) 

My code so far works when none are checked and when one is checked, but returns nothing when more than one check-box has been checked. So to try and fix this I altered the code to this

WHERE 
(EffectiveDate BETWEEN @StartDate AND @EndDate) 
AND ((((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR @IncludeSEWPrefix = 0)
OR ((@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR @IncludePAWPrefix = 0)
OR ((@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR @IncludeRPLPrefix = 0)))

Which resulted in all rows being returned no matter what was selected. Can someone tell me where I am going wrong?

3 Answers 3

1

I believe this is the correct rearrangement. Trickier problem than it first appears. The issue was seperating lines like ((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR @IncludeSEWPrefix = 0) with AND meant that if two includes were true, a row would need to have both PrefixId's, which can't happen. And if you separated them with OR, then having just one include false, means that every row will pass. So instead, check that a row has the prefix of any that are included, otherwise all includes have to be off.

WHERE EffectiveDate BETWEEN @StartDate AND @EndDate
AND 
(
    (@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR
    (@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR 
    (@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR
    (@IncludeSEWPrefix = 0 AND @IncludePAWPrefix = 0 AND @IncludeRPLPrefix = 0)
)
1
  • Thank you your solution has worked perfectly and your explanation was very clear Jul 17, 2012 at 13:11
0

Try this

WHERE  
(EffectiveDate BETWEEN @StartDate AND @EndDate)  
AND 
(
(@IncludeSEWPrefix = 1 AND PrefixId = 'SEW' OR @IncludeSEWPrefix = 0) AND 
(@IncludePAWPrefix = 1 AND PrefixId = 'PAW' OR @IncludePAWPrefix = 0) AND 
(@IncludeRPLPrefix = 1 AND PrefixId = 'RPL' OR @IncludeRPLPrefix = 0)
)  
1
  • Thanks for your reply, but it didn't work. I thing your code is the same as my first solution but with less commas. Jul 17, 2012 at 10:32
0

You have more parenthesis than needed, it does not hurt but just be aware. Maybe this can help:

WHERE (EffectiveDate BETWEEN @StartDate AND @EndDate) 
    AND (   ((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR (@IncludeSEWPrefix = 0 AND @PrefixId <> 'SEW'))
        OR ((@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR (@IncludePAWPrefix = 0 AND @PrefixId <> 'PAW'))
        OR ((@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR (@IncludeRPLPrefix = 0 AND @PrefixId <> 'RPL'))
        )
3
  • Thanks for reply, but this code produces the same result as my first example. If two of the vars are true it needs to return the the rows that match either string Jul 17, 2012 at 10:45
  • No sorry, this does the same as my second example. It returns all rows regardless of what has been checked. Jul 17, 2012 at 10:55
  • Can you add a script with sample data to test?
    – Yaroslav
    Jul 17, 2012 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.