I have a need to filter records where:
NotificationRead = 0 || NULL --> IF GetRead = 0
NotificationRead = 1 --> IF GetRead = 1
NotificationRead = 0 || 1 || NULL --> IF GetRead = NULL
Here is the query I am using for this:
DECLARE @GetRead BIT
DECLARE @Query VARCHAR(20)
SET @GetRead = NULL
IF @GetRead = 0 SET @Query = '0,NULL'
ELSE IF @GetRead = 1 SET @Query = '1'
ELSE SET @Query = '0,1,NULL'
SELECT * FROM vwNotifications WHERE NotificationRead IN (@Query)
The query above basically fails when I supply NULL
in the IN
clause.
I do know the reason why thanks to this question.
But if I take an approach as suggested in that question's answer (using NotificationRead IN (@Query) OR NotificationRead IS NULL
), I get all records where NotificationRead = NULL
when I don't need them for example, when @GetRead = 1
Can you please point me to the right direction here?