I am trying to do something that should be fairly simple but ISNULL isn’t doing what I thought it would.
Basically I have a stored procedure and I am expecting either PARAM1 OR PARAM2 to have a matching value in my table.
SELECT * FROM MyTable WITH (NOLOCK)
WHERE
field1 = ISNULL(@PARAM1 ,field1 )
AND
field2 = @PARAM2
This works fine until I have NULL fields in my row then it excludes those results. Is there a different method that can cater for this?
ISNULL replaces the first value with the second value, so only if your parameter @PARAM1 is NULL does it replace it with PARAM1. I assume you’re not passing in NULL values, so that’s probably not what you want. More likely you just want to say
I Suppose you could use ISNULL in this fashion too: