Skip to content
Advertisement

How to handle True, False or NULL in where clause

How can I reduce this to only one where statement @state as bit

IF @state IS NULL
    SELECT * FROM TBL WHERE name IS NULL
ELSE
    SELECT * FROM TBL WHERE name = @state

I wish I could compare True, False or NULL in one select statement

Advertisement

Answer

Would this not work?

SELECT * 
FROM TBL 
WHERE (@state is null and name is null) or 
      (name = @state)

Another approach would be to use coalesce on both conditions.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement