Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi,

Can anyone please tell me the difference between isnull function is != operator.

What I have tried:

For Example
I want to find employees who do not have manager or whose manager is NULL.
What should i use ISNULL function or != operator and which approach is good? and why?

EmpID	EmpName	ManagerID
1	Gayatri	1
2	Radhika	[NULL]
3	Chetan	[NULL]
4	Ankur	5
5	Chhuts	11
Posted
Updated 25-May-17 21:48pm
v2
Comments
PIEBALDconsult 25-May-17 20:31pm    
Yes.
Robert Welliever 25-May-17 20:56pm    
Just curious, if the employees who don't have a manager are null, how else are you designating employees who do not have a manager? It seems you would only need to check for null. If you are just checking for null you might go with what is most common, i.e.:

SELECT * FROM Employees WHERE ManagerID IS NULL
Robert Welliever 26-May-17 17:05pm    
I see the solution was selected, but the solution did not address your question accurately in that checking IS NULL and using the ISNULL function are two different things. You might want to understand that nuance. Also on a best practices level, don't use !=, instead use <>. Many large organizations (like Microsoft) deprecated the use of != (although still supported). Although I'm not entirely sure why it was deprecated (maybe SQL-92 Standard?) I wouldn't create habits against best practices without cause.

You should use IS NOT NULL instead of using !=,

!= will not going to provide a result

If you want to use != then You should use
SELECT * FROM MyTable WHERE ISNULL(Manager,0) != 0

Let me know if you want further more explanation.
 
Share this answer
 
v2
Try it.
You will get different results...
SQL
SELECT * FROM MyTable WHERE Manager != NULL
Will give you no rows.
SQL
SELECT * FROM MyTable WHERE Manager IS NOT NULL
Will give you rows where there is a manager.
Why? Because according to W3Schools[^] "It is not possible to test for NULL values with comparison operators, such as =, <, or <>."
NULLs propagate through expressions - anything involving a NULL value results in NULL. So when you use a condition involving an equality operator and one of the sides is NULL, the result is not TRUE or FALSE, it's NULL.
In fact, both of these will produce the same results:
SQL
SELECT * FROM MyTable WHERE Manager != NULL

SQL
SELECT * FROM MyTable WHERE Manager = NULL
Both give you no rows!
 
Share this answer
 
Comments
Maciej Los 26-May-17 2:13am    
Well explained, 5!
CPallini 26-May-17 3:25am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900