Checking an input param if not Null and using it in where in SQL Server Checking an input param if not Null and using it in where in SQL Server sql-server sql-server

Checking an input param if not Null and using it in where in SQL Server


You can use IsNull

 where some_column = IsNull(@yourvariable, 'valueifnull')

EDIT:

What you described in the comment can be done like:

where (@code is null or code = @code)


How about

WHERE (Column1 = @Var1 OR @Var1 IS NULL)AND (Column2 = @Var2 OR @Var2 IS NULL)


Here's another approach

SELECT * FROM Thingies WHERE ( @thingId IS NULL OR ThingID = @thingId )