How do the SQL "IS" and "=" operators differ?

37,366

Solution 1

You want records from Foo where Bar = @param, or if @param is null, where Bar is null. Some of the proposed solutions will give you null records with nonnull @param, which does not sound like your requirement.

Select * from Foo where (@param is null and Bar is null) or (Bar = @param)

This doesn't say whether this is Oracle or SQL Server or another RDBMS, because they each implement slightly different helper functions. SQL's ISNULL(first, second) like NVL(first, second). I like SQL Server's COALESCE() for the general applicability.

The IS comparison is only for null comparisons.

If you are using SQL Server and if you really need a different 3VL logic truth table to solve your problem (that is, if you have a specific need for "NULL=NULL" to be "true" at some point in time, and also recognize that this is deprecated and barring your reasons, not a good idea in general), within your code block you can use the directive

SET ANSI_NULLS OFF

Here's the BOL on it: http://msdn.microsoft.com/en-us/library/ms188048.aspx

Solution 2

You may be thinking about this incorrectly. If you're talking about SQL Server, for example (since that's what I have to hand), your second example will result in a syntax error. The value on the right-hand side of IS cannot be 5.

To explain, consider MSDN's explanation of these two operators in T-SQL (note that asking about "SQL" and about "SQL Server" are not necessarily the same).

Equals (=) operator

IS NULL operator

Notice something important, there. There is no such thing as the "IS" operator in T-SQL. There is specifically the <expression> IS [NOT] NULL operator, which compares a single expression to NULL.

That's not the same thing as the = operator, which compares two expressions to each other, and has certain behavior when one or both of the expressions happens to be NULL!

Solution 3

Edit: (Update from OP: This doesn't do what I If @param is 5, then I want to see only records where Bar is 5. I want to see records where Bar is NULL if, and only if, @param is NULL. I apologize if my question didn't make that clear.)

In that case, I think you should try something like this:

SELECT * FROM Foo WHERE Bar=@param OR (Bar IS NULL AND @param IS NULL)

Previous post:

Why not simply use OR ?

SELECT * FROM "Foo" WHERE "Bar"=@param OR "Bar" IS NULL

In SQL Server, you can use ISNULL:

SELECT * FROM "Foo" WHERE ISNULL("Bar",@param)=@param

Solution 4

I don't know what version of SQL you are using but IS makes no sense in the context you just described. I get a syntax error if I try to use it the way you described. Why would you want to use it over = anyway? This is the common usage and the one software maintainers woudl expect to find.

Solution 5

What specific database are you using?

If you're doing searches based on null (or not null), using IS is the way to go. I cannot provide a technical reason but I use this syntax all the time.

SELECT * FROM Table WHERE Field IS NULL

SELECT * FROM Table WHERE Field IS NOT NULL
Share:
37,366
Dan Moulding
Author by

Dan Moulding

Updated on July 09, 2022

Comments

  • Dan Moulding
    Dan Moulding almost 2 years

    I am building some prepared statements that use parametrized values. As an example:

    SELECT * FROM "Foo" WHERE "Bar"=@param
    

    Sometimes @param might be NULL. In such cases, I want the query to return records where Bar is NULL, but the above query will not do that. I have learned that I can use the IS operator for this. In other words:

    SELECT * FROM "Foo" WHERE "Bar" IS @param
    

    Aside from the differing treatment of NULL, are there any other ways in which the above two statements will behave differently? What if @param is not NULL, but is instead, let's say, 5? Is using the IS operator in that case a safe (and sane) thing to do? Is there some other approach I should be taking?

  • Dan J
    Dan J about 13 years
    Not to be a contrarian, but, while it does provide alternatives, this doesn't actually answer the OP's question, i.e. what is the difference between the IS and = operators...
  • Hari Menon
    Hari Menon about 13 years
    "In such cases, I want the query to return records where Bar is NULL, but the above query will not do that." .. "Is using the IS operator in that case a safe (and sane) thing to do? Is there some other approach I should be taking?" I believe the OP is primarily interested in a safe way to achieve this. I really don't know the difference between those except that IS is meant for NULL and I think that that was the secondary question.
  • Dour High Arch
    Dour High Arch about 13 years
    I do not think this is what the OP wanted; this always returns rows where Bar IS NULL, while the question says "Sometimes @param might be NULL. In such cases, I want the query to return records where Bar is NULL". Not the same thing.
  • Dan Moulding
    Dan Moulding about 13 years
    This doesn't do what I want. If @param is 5, then I want to see only records where Bar is 5. I want to see records where Bar is NULL if, and only if, @param is NULL. I apologize if my question didn't make that clear.
  • Dan Moulding
    Dan Moulding about 13 years
    I am using SQLite and it let's me put 5 on the right hand side of IS and behaves as I would intuitively expect.
  • Dan Moulding
    Dan Moulding about 13 years
    I want to use it over = precisely because I need the query to return results where Bar is NULL if @param is NULL. The = operator won't do that. It doesn't result in a syntax error on the system I'm using (SQLite).
  • Dan Moulding
    Dan Moulding about 13 years
    I am using SQLite, and it allows me to use IS to compare with non-null values. Can you cite a source that says that in standard SQL the "IS" operator is only for null comparisons?
  • Dan Moulding
    Dan Moulding about 13 years
    I am using SQLite. The problem is I don't know whether I will be comparing to NULL or not. I am comparing to @param. At runtime, @param will sometimes be NULL, and at other times it will be non-NULL.
  • Dan Moulding
    Dan Moulding about 13 years
    Can you cite a source that says that, in standard SQL, 5 can't be put on the right hand side of IS? Or are you saying that IS isn't even standard SQL?
  • Dan Moulding
    Dan Moulding about 13 years
    Can you cite a source that shows that my use of IS is a syntax error in standard SQL? (I'm using SQLite, which allows it, but maybe that's a non-standard extension).
  • user662852
    user662852 about 13 years
    Actually, I can: in the SQL-92 standard (free on the web), IS is a boolean operator. Both sides are be boolean, by ANSI standard. Null is a boolean value in 3VL logic. Search this document for "8.12 <search condition>": contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt,
  • user662852
    user662852 about 13 years
    And, I should point out, any RDBMS vendor is free to implement the standard or add additional elements however they like. So if SQLLite wants to provide a syntactic sugar, that's their business. It's not like SQL Server or Oracle get the full standard correct themselves.
  • dburges
    dburges about 13 years
    IS only works if the value is null in standard SQL. I Have never used SQL lite, but have you tried it when you have a numeric or string value insted of null?
  • Dan J
    Dan J about 13 years
    Source for standard IS NULL/IS NOT NULL predicate (as opposed to IS operator). I have yet to find reference to IS as a standard SQL operator, though the SQLite documentation implies it is a standard reserved word.
  • Dan Moulding
    Dan Moulding about 13 years
    Thank you for the source. So, it sounds like there is a quite a big difference between the two operators in standard SQL. In SQLite SQL (which I am using) it sounds like the only difference is the treatment of NULL. Thank you for your answer!
  • Dan J
    Dan J about 13 years
    Yeah, you've solved both the specific issue and defeated my ability to locate the ANSI SQL standard - you've got my upvote as well. :)