Reputation: 135
Can someone guide me on how to approach the following:
x CHAR:= 'Y'; --VARIABLE
SELECT a.id, a.name, a.description
FROM Table1 a
WHERE (IF x = 'Y' THEN
a.name = parameter /*FILTER BY NAME*/
ELSE
/*BRING ALL (NO NAME FILTER*/);
I'm trying to do a conditional WHERE
CLAUSE. IF x = 'Y'
Then filter by name; Else bring all names... what would be the best method for performing the above?
Upvotes: 2
Views: 1579
Reputation: 16917
You just need to include both conditions in an OR
statement to do this:
SELECT a.id, a.name, a.description
FROM Table1 a
WHERE (x != 'Y' OR
(x = 'Y' AND a.name = parameter))
If X
is anything other than Y
, all records will be pulled, otherwise, if X
is Y
, it will also filter on a.Name
.
Upvotes: 3