0
Declare @param char(1)
SET @param = 'A'

@param can be 'A','B' or 'C'

SELECT x, y FROM myTbl
WHERE y = @param

if @param = 'A' or 'B' then y = @param works fine if @param = 'C' then I want to WHERE to fetch the rows where y IS NULL

I could achieve this using dynamic sql but can't figure out how to do it without dynamic sql. Any thoughts?

1
  • Which RDBMS is this for? Please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely. Commented Oct 28, 2016 at 20:56

1 Answer 1

1

Use basic boolean logic:

SELECT x, y
FROM myTbl
WHERE y = @param or (@param = 'C' and y is null);

If you really want to be more explicit:

SELECT x, y
FROM myTbl
WHERE (@param <> 'C' AND y = @param) OR
      (@param = 'C' AND y IS NULL);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.