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?
The SQL AND, OR and NOT Operators The AND and OR operators are used to filter records based on more than one condition: The AND operator displays a record if all the conditions separated by AND are TRUE. The OR operator displays a record if any of the conditions separated by OR is TRUE.
Difference between SQL Not Equal Operator <> and != to do inequality test between two expressions. Both operators give the same output. The only difference is that '<>' is in line with the ISO standard while '!= ' does not follow ISO standard.
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
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!
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