Sewder
Sewder

Reputation: 754

Filter query by one parameter or two

I'm trying to create a report where the user can select either parameter option and it will filter by chosen parameter, or filter by both.

Currently I'm using an OR statement like so:

   SELECT a,b
   FROM table a
   WHERE (a = @A) OR (b=@b)

The issue is when I try and enter a value for both it only filters by one. How do I change it so it will filter each one out but at the same time keep the ability to filter by only one?

Upvotes: 0

Views: 109

Answers (1)

sakir
sakir

Reputation: 3502

WHERE (a = @A or @A IS NULL) AND (b=@b OR @b IS NULL)

Upvotes: 4

Related Questions