DaveInIowa
Programmer
Is there a better way to code the following comparison in a WHERE clause?
Normally when I accept a null parameter value I use it to mean I don't want to filter on that particular value but in this case I want it to mean I only want records where the ColumnValue is also null.
I thought about doing this...
where [red]'text'[/red] is a value that will never be in the table but I don't like using magic values and you never know what values will never be in the table. I considered converting a NEWID() to a VARCHAR(50) and using that as the [red]'text'[/red]. There has to be a more elegant solution.
Code:
WHERE ((@ParameterValue IS NULL AND ColumnValue IS NULL) OR (@ParameterValue = ColumnValue))
Normally when I accept a null parameter value I use it to mean I don't want to filter on that particular value but in this case I want it to mean I only want records where the ColumnValue is also null.
I thought about doing this...
Code:
WHERE ISNULL(@ParameterValue, [red]'text'[/red]) = ISNULL(ColumnValue, [red]'text'[/red])
where [red]'text'[/red] is a value that will never be in the table but I don't like using magic values and you never know what values will never be in the table. I considered converting a NEWID() to a VARCHAR(50) and using that as the [red]'text'[/red]. There has to be a more elegant solution.