I know that comparison operators don't work with null values in SQL so for that we use isnull, Now I have a situation with a table like this,
ID Name Order
3 KnowledgeBase1 NULL
4 KnowledgeBase2 NULL
5 KnowledgeBase3 NULL
6 KnowledgeBase4 NULL
7 Child of first 1
Now here you can see that Order can be null or any numeric value,
Now here's the select query
declare @order int = null
select
ID,
Name,
Order
from tbl
where Order = @order
this query is good when @order has any numerals but when it has null it doesn't work of course because where clause should be Order is null for that case,
So question is that is there any way to do this without using exec() with a string of query or a conditional statement in my query ??
Compare @order to NULL in the where clause, and if it is, use order IS NULL and OR that together with your existing condition that works if it has a value:
select
ID,
Name,
Order
from tbl
where
Order = @order
OR (@order IS NULL AND order IS NULL)
You have not specified your RDBMS, but a quick test shows this working with MySQL...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With