difference between COMM IS NULL and COMM = NULL

ali zubair 21 Reputation points
2022-10-01T22:42:51.927+00:00

I have a job interview they asked me what is the difference between is null and = null in sql

for example,

select * from emp where comm = null

OR

select * from emp where comm is null

in SQL Server if we set SET ANSI_NULLS ON the COMM =NULL does not show any result, can any one explain this, thanks

Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    2022-10-01T22:50:02.077+00:00

    Hi @ali zubair

    Here is a really nice answer to your question:

    What's the difference between " = null" and " IS NULL"?

    https://stackoverflow.com/questions/2749044/whats-the-difference-between-null-and-is-null

    In a WHERE clause, column=null will never be true, it is not valid to use null this way, you need to say column IS NULL or column IS NOT NULL. This speaks to the special nature of NULL, it is not a value to check for equality of, it is an unknown value, so you need to use the IS or IS NOT syntax.

    You may assign something the NULL value using the = equal. For example: UPDATE TableX SET Column=NULL...

    (Answered by "anonymous user")

    -----------------------------

    If this is helpful please accept answer.


2 additional answers

Sort by: Most helpful
  1. Dan Guzman 9,401 Reputation points
    2022-10-01T22:57:07.877+00:00

    A comparison in SQL may return TRUE, FALSE, or UNKNOWN. With the equality operator, the result is is TRUE when values are the same, FALSE when different, and UNKNOWN if either or both values NULL.

    NULL values are not returned because the WHERE expression result is UNKNOWN instead of TRUE for every row due to the NULL literal. To check for NULL in SQL, one must use the IS NULL operator instead of =:


  2. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-10-02T01:24:31.507+00:00

    Where did you set this?

    SET ANSI_NULLS ON

    Can you post the code (sample to show us what you got).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.