SQL Queries using Null fields -- Must use "is null" or "is not null"?
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 2K views
Discussion
Physics news on Phys.org
- 19,978
- 11,076
Depends if that factor is important for your result. I don't think there is a requirement. Are you getting errors?
Science Advisor
Homework Helper
- 7,831
- 13,158
Hi Greg, no, I am learning the topic and the source material did not seem clear on this, i.e., on whether we should use, for the null field 'field' either "where field is null" , or " where field is not null".Greg Bernhardt said:Depends if that factor is important for your result. I don't think there is a requirement. Are you getting errors?
- 19,978
- 11,076
Again depends on what result you are after. If you want to factor in nulls for a field then use IS NULL, if you want to factor in records that have a field that is not null, then use IS NOT NULL. If you don't care, then don't add the clause :)WWGD said:whether we should use, for the null field 'field' either "where field is null" , or " where field is not null".
Mentor
- 15,785
- 10,661
Here's more about SQL nulls
http://www-cs-students.stanford.edu/~wlam/compsci/sqlnulls
Null behavior is kind of funny compared to regular programming languages especially in using equality tests or in table joins.
http://www-cs-students.stanford.edu/~wlam/compsci/sqlnulls
Null behavior is kind of funny compared to regular programming languages especially in using equality tests or in table joins.
Mentor
- 4,789
- 3,854
NULL is not equal to anything - including itself - what jedishrfu is telling you. When you employ a where clause you are looking for a resultset that matches what you are looking for. There may be some 'yellow' values but you do have to say '!= yellow', just say '=red' instead.
So you can ignore NULLS
Except when the select statement uses some kind of function and operates on a field that can be NULL. You cannot have a NULL in a function, so you must either exclude those rows or wrap the result in a default NVL() function to transform the NULL to something usage.
In this pretend example upper will barf if column_name returns a NULL, so you throw some 'dummy' value out there to feed it a usable value when the column_name is NULL.
All indexed columns in a table should have the NOT NULL property. For the same reason.
So you can ignore NULLS
Except when the select statement uses some kind of function and operates on a field that can be NULL. You cannot have a NULL in a function, so you must either exclude those rows or wrap the result in a default NVL() function to transform the NULL to something usage.
Code:
select UPPER(NVL(column_name, 'dummy')) from some_table;
In this pretend example upper will barf if column_name returns a NULL, so you throw some 'dummy' value out there to feed it a usable value when the column_name is NULL.
All indexed columns in a table should have the NOT NULL property. For the same reason.
newjerseyrunner
- 1,532
- 637
When people first get into computer science, they have trouble with certain special values. The concept that x = x is not necessarily true is hard to grasp, it's even worse, when that equal sign doesn't even resolve to true or false.
In most computer languages NULL is a constant, and it does equal itself. That's not true in SQL.
PHP
Will spit out
true
true
true
false
false
falseMySQL
Will spit out
NULL
1
NULL
NULL
1
0
In most computer languages NULL is a constant, and it does equal itself. That's not true in SQL.
PHP
Code:
echo (NULL == NULL) ? 'true' : 'false';
echo (NULL === NULL) ? 'true' : 'false';
echo (NULL == 0) ? 'true' : 'false';
echo (NULL === 0) ? 'true' : 'false';
echo (NULL == 1) ? 'true' : 'false';
echo (NULL === 1) ? 'true' : 'false';
true
true
true
false
false
falseMySQL
Code:
SELECT NULL = NULL;
SELECT NULL IS NULL;
SELECT NULL = 0;
SELECT NULL = 1;
SELECT 0 = 0;
SELECT 0 = 1;
NULL
1
NULL
NULL
1
0
Similar threads
Is the use of null an anti-pattern?
- SlurrerOfSpeech
- · Replies 4 ·
- Programming and Computer Science
- Replies
- 4