jack
jack

Reputation: 67

SQL WHERE clause with parameter - NULL should match depending on parameter value

I am writing a query with a parameter.

So far, I only have something like this:

Select * From Table a  
Where a.Column1 = @Value

Upvotes: 0

Views: 260

Answers (1)

forpas
forpas

Reputation: 164089

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)

Upvotes: 1

Related Questions