I’m working on a bigger problem, but have narrowed it down to this, in order to make this question easier to ask.
The following query:
Select * from User where Country = Country
Will only return the rows where Country is not null. I would expect if Column1 is null it would return it because NULL = NULL.
Any input on why this is not working as I expect?
EDIT:
I am trying to do this:
Select * from User where Country = coalesce(@Country, Country)
If my variable @Country is null, I want it to pull everything.
SQL uses three valued logic (
true,false,unknown) and comparisons toNULL(except for testing with theIS [NOT] NULLoperator) evaluate asunknownnottrueas long as the session optionANSI_NULLSis on.For a row to be returned from your query the
WHEREclause needs to evaluate totrue.Following your edit you can use
To return all rows when
@country is null. Although probably better just to break these up into 2 cases to avoid the recompile penalty.