I would like to SET 'IS NULL' as a variable and use it in a WHERE clause.
I will be unioning many tables together that carry the same WHERE clause and would like flexibility to use a variable for my yearCompleted column – see example below-
DECLARE @YEARCOMPLETED
SET @YEARCOMPLETED = NULL
SELECT * FROM TABLE1
WHERE yearCompleted = @YEARCOMPLETED
UNION ALL
SELECT * FROM TABLE2
WHERE yearCompleted = @YEARCOMPLETED
In this instance is it possible to find rows where yearCompleted IS NULL using a variable?
Best Answer
This isn't the most elegant solution, but it'll work to return matching rows if
@YEARCOMPLETED
is a value, or if it is null. This works decently because it's able to make use of an index on theyearCompleted
column, but it basically doubles your code with a near duplicate:If you don't care about SARGability and using an index on that column, you could do this. It's less duplicated code, but probably with worse performance:
The most elegant solution, to balance reducing duplicated code and good performance, is to use dynamic SQL: