Skip to content
Advertisement

Sql Where clause, if parameter equals this value, grab it and null values as well? [closed]

Hello I need help with this query I am writing in which if one of my parameters equals this value I want to grab it and get all the null values as well. I’m not quite sure how to go about writing this query. For example if my parameter is 1, then grab where all in the column are 1 and nulls as well, if value is 2 only get where value is 2, nothing else.

Select * From Table a  //I have it something like this.
Where a.Column1 = @Value

Advertisement

Answer

What you want is all the rows where a.Column1 = @Value and nulls only if @Value = 1:

SELECT * 
FROM tablename
WHERE (@Value = 1 AND Column1 IS NULL)
   OR (Column1 = @Value)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement