I've a Status column in a table which has 3 values - 'N/A' , 'Single' ,'Multiple'. Some rows have a NULL value for the Status column.
I need to pull up all the rows in which Status is not null and is not 'N/A'. Basically, I need all the rows whose status is "Single" or "Multiple".
I've been just reading up about NULL actually being equivalent to 'UNKNOWN'.
If I say
SELECT *
FROM t_userstatus
WHERE status <> 'N/A'
I get the results (All rows containing "Single" or "Multiple" only).
What I would like to know is that , does the above WHERE clause always exclude the rows having NULL values?Is that the expected behaviour?
What causes this to exclude null rows even though I've not explicitly specified it?
In my query,do I have to explicitly say status IS NOT NULL ?
I am relatively new to programming, any help is appreciated.
It is not normal behavior as N/A (Single & Multiple as well) is a string that is not related to NULL. Even though the NULL is evaluated to unknown and may not return, you should explicitly use IS NOT NULL.
SELECT [column_list] FROM t_userstatus
WHERE status IS NOT NULL AND status <> 'N/A'
I would also recommend you get in the habit of specifying a column list.
SQL uses three-valued logic: true, false, and unknown. Any comparison to null results in unknown.
So null <> 'N/A' evaluates to unknown. Since unknown is not true, that means the row gets excluded.
Yes, that is the normal behavior. A NULL value has no value, so it does not satisfy a WHERE condition. If you want to also include null values, you need to specify IS NOT NULL as well.
As others have said, null is generally not comparable. So, a status != 'N/A' comes back as false when status is null.
But what others haven't mentioned yet is that that is controlled through the Ansi_nulls setting. By default it is on and nulls are not comparable (as you state in the question, the principal behind this is that they are unknown so it is false that they are not necessarily N/A). But you can use
SET ANSI_NULLS OFF
To change this behavior in which case a null will be equal to a null and not equal to anything else. There is more details on MSDN here.
A bit non-obviously, SQL has three valued logic (true/false/unknown). Any direct comparison with NULL will result in the value unknown which in the WHERE clause is considered non-true. Your condition;
WHERE status <> 'N/A'
will in other words never match a null since NULL is never "not equal" to anything.
The only ways to match a NULL using a comparison is using the special null operations IS NULL or IS NOT NULL.
As a side note, life is naturally never quite that simple.
Related
This question already has answers here:
SQL not displaying null values on a not equals query?
(5 answers)
Closed 20 hours ago.
Before select
Status could be:
X1, X2, NULL
SELECT * FROM surrtest t Where t.Status != „X2“
It’s deleting also rows where Status was null. But it shouldn’t.
So after query I’m only getting rows where status Is X1 but I also want the NULL rows.
Is it possible?
I don’t know if it’s possible. If not maybe there is a workaround where we replace the null values with empty strings? But I hope it is possible without changing the data.
This should work:
SELECT * FROM surrtest Where Status != ‘X2’ or Status is null
If that doesn’t work then there’s something weird going on with your database.
In most databases, a where clause evaluates to either true, false, or unknown. Any comparison with null will result in an unknown result. Since a where clause filters out all records that don't evaluate to true, you will not see records that evaluate to unknown, as well as false.
In your original query, the where clause was filtering out null records, because they evaluated to unknown. So, you have to add the extra part or Status is null to the where clause to include the null records.
Also, do NOT use or Status = null, since that will always evaluate to unknown, like mentioned before. You must use or Status is null.
I am having problem while understanding = and is operators in SQL Server.
Consider the following example queries which are having different behaviors in their respective output:
SELECT * FROM tableName WHERE colName IS NULL;
SELECT * FROM tableName WHERE colName = NULL;
First query will provide the required output i.e. select those records for which colName is having a null value. But the second query will result in zero matching records.
Please clarify different uses of these operators with pros and cons.
EDIT
Here, most of the answers are claiming that = doesn't work with null, but the following statement will work with null and =.
SET ANSI_NULLS OFF
SELECT * FROM tableName WHERE colName = NULL;
This will provide the same result as statement having is operator.
Nothing equals null.
Not even null equals null.
null is not a value, it is more like a concept, or a mark, meaning unknown value.
As such, you need two operators for this, one for equality, and one for checking the concept of null.
Once you start to think of null as "unknown value" a lot of the other behavior also makes sense.
10 + null? Add an unknown value to 10? Obviously you will have another unknown value as a result.
For more information, please check the documentation of the equality operator in T-SQL.
Additionally, see the documentation for SET ANSI_NULL.
Note that the documentation is in conflict about the behavior of x = null between the equality operator (documentation says it will always be false if x is non-null) whereas SET ANSI_NULLS documentation says that x = null will behave equivalent to x is null when the option is turned on.
From http://msdn.microsoft.com/en-us/library/ms188795.aspx:
To determine whether an expression is NULL, use IS NULL or IS NOT NULL instead of comparison operators (such as = or !=). Comparison operators return UNKNOWN when either or both arguments are NULL.
EDIT
The originating question was updated to note that SET ANSI_NULLS OFF allows:
select * from tableName where colName = null.
This may be true at the moment but future versions of SQL server will set ANSI_NULLS always ON and any calls to SET ANSI_NULLS OFF will result in an error.
Source: http://msdn.microsoft.com/en-us/library/ms188048.aspx
= NULL is always unknown (this is piece of 3 state logic), but WHERE clause treats it as false and drops from the result set. So for NULL you should use IS NULL
= NULL is used for assignment to a NULL value whereas IS NULL is used to determine whether a variable is NULL-valued.
See these articles on null
Wikipedia NUll (SQL)
w3schools SQL NULL Values
SQL Tutorial, see IS NULL Operator section
Null is not same as zero. Null is absence of value. It simply means that the value could be anything. It is unknown.
Q. Why select * from tableName where colName = null returns zero rows?
A. Because you can not be sure that one null(unknown value) is equal to or not equal to another null(another unknown value).
EG:
I have a magic hat and nobody knows what will come out of it(if anything comes out at all).
You have another magic hat and nobody knows what will come out of it(if anything comes out at all).
We both have a magic hat each and what it has inside is unknown (null).
These both hats could contain a rabbit each or may be my hat contains a hammer and your's has a pineapple.
If you have an if condition like this..
if(#flag<>null)
it is a mistake because if you pass null to #flag you do not really know whether or not #flag is equal or not equal to null
Use if(#flag<>isnull(null,'')) instead
I am confuse about what is the value of NULL in SQL. NULL I think is empty.
So will it contain any garbage value or unknown value or like 0 in integer or blank in character?
In simple worlds you can say that Null is not a data value, but a marker for an unknown value.
So any mathematical operations performed on NULL will result in NULL. For example,
10 + NULL = NULL
Similarly if you do string concatenation with string you get:-
'String ' || NULL || 'Concatenation' -- Result is NULL
So you can say that Null means either "not applicable" or "don't know": it is not the same as zero (0) or any other default value, but more importantly, null is treated quite differently from other values in SQL, because it literally has no value.
An example to explain what it means when we say that NULL means UNKNOWN VALUE:
StudentName TestResult
X 78
A 89
B 67
C NULL
So you can see that the student C got NULL marks in the test. So what
does that mean?
Now one can say that the student does not sit in the test or it may be that the student's data is not avaialable. But it definitely does not mean that the student got 0(as if you assign 0 to the student then it would mean that the student appeared in the test and got zero) marks in the test. All we can say that the data for the student is UNKNOWN or NULL
A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different than a zero value or a field that contains spaces.
If a column in a table is optional, we can insert a new record or update an existing record without adding a value to this column. This means that the field will be saved with a NULL value.
NULL values are treated differently from other values.
NULL is used as a placeholder for unknown or inapplicable values.
Read more about this here.
NULL is a keyword used to represent unknown/missing data.
Say you have an optional column in your table. You can insert a new record or update an existing record without adding a value to this column. This means that the field will be saved with a NULL value.
Check this for more details.
It is to indicate that a data value does not exist in the database.NULL is also an SQL reserved keyword used to identify the Null special marker.
NULL means “a missing unknown value” and it is treated somewhat differently from other values.
A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different than a zero value or a field that contains spaces.
10 * NULL -- Result is NULL
Null (SQL)
SQL NULL
Working with NULL Values
Null is not empty actually but it is considered as uknown value, from dumentation
Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values.
You can ready more here
I am getting different results when I run the query with above clause but not able to understand why. can any one explain what is the difference between the two clauses.
The result of column = null is unknown (null), since it can't be known what null really is. If you want to test for null and get a boolean value back you need to use is null. So, `column` is null is the correct syntax to use.
A comparison to null always evaluates to false, so column = null evaluates to false as well as column != null, independently of the value of column. If you want to actually check whether a value is null, you have to use column is null.
In my table, I have a nullable bit column (legacy system...) and another developer recently made a change to a stored procedure to only show values where the bit column was not true (1). Because this is a nullable column, we noticed that if the column was NULL, the record was not being picked up. WHY is this?
Both the other developer and I agree that NULL <> 1... Is this a bug in SQL or was this designed this way? Seems like a design flaw.
Current Code:
(VoidedIndicator <> 1)
Proposed Fix:
(VoidedIndicator <> 1 OR VoidedIndicator IS NULL)
Clarification (By Jon Erickson)
VoidedIndicator is a nullable bit field so it can have the following values: NULL, 0, or 1
When a SQL statement is created with a where clause such as (VoidedIndicator <> 1) we only get records returned that have VoidedIndicator == 0, but we were expecting both VoidedIndicator == 0 and VoidedIndicator IS NULL. Why is this?
Lots of good answers, but let me give you a really concise version.
To SQL, Null does NOT mean "No value" it means "Unknown Value"
With that in mind, consider the answer to the question you are asking SQL in plain English.
Q: Is this unknown value not equal to 1?
A: I don't know, there is no way to tell without knowing the value.
Hence Null<>1 = Null
From the Wikipedia entry on NULL:
For example, a WHERE clause or
conditional statement might compare a
column's value with a constant. It is
often incorrectly assumed that a
missing value would be "less than" or
"not equal to" a constant if that
field contains Null, but, in fact,
such expressions return Unknown. An
example is below:
-- Rows where num is NULL will not be returned,
-- contrary to many users' expectations.
SELECT * FROM sometable WHERE num <> 1;
Basically, any comparison between NULL and something else, whether it's with = or <> will not be true.
As another reference, the MSDN T-SQL page on <> states:
Compares two expressions (a comparison
operator). When you compare nonnull
expressions, the result is TRUE if the
left operand is not equal to the right
operand; otherwise, the result is
FALSE. If either or both operands are
NULL, see SET ANSI_NULLS
(Transact-SQL).
The SET ANSI_NULLS page then states:
When SET ANSI_NULLS is ON, a SELECT
statement that uses WHERE column_name
= NULL returns zero rows even if there are null values in column_name. A
SELECT statement that uses WHERE
column_name <> NULL returns zero rows
even if there are nonnull values in
column_name.
...
When SET ANSI_NULLS is ON, all
comparisons against a null value
evaluate to UNKNOWN. When SET
ANSI_NULLS is OFF, comparisons of all
data against a null value evaluate to
TRUE if the data value is NULL.
It's not a bug.
NULL is not equal to anything, not even NULL (NULL = NULL returns FALSE).
Typically NULL values aren't indexed either. It's generally a bad idea to rely on a particular value or NULL. Depending on what you're storing in the column, you may be better off putting a dummy or sentinel value in rather than using NULL to indicate some meaning.
The other folks are correct that NULL <> 1 doesn't evaluate as true, therefore it doesn't satisfy the WHERE clause.
The proposed fix you describe is the best way of handling it:
(VoidedIndicator <> 1 OR VoidedIndicator IS NULL)
SQL-99 does have a predicate that helps in this case, called IS DISTINCT FROM:
(VoidedIndicator IS DISTINCT FROM 1)
This predicate would behave exactly the same as your proposed fix. Unfortunately, Microsoft SQL Server does not support IS DISTINCT FROM yet.
You can also: isnull(VoidedIndicator,1) <> 1
Because the WHERE clause only selects rows when the condition evaluates to true.
When one of the operands is NULL, the condition usually evaluates to UNKNOWN (approximately equivalent to NULL), and therefore is not true. It applies to both 'column = 1' and 'column <> 1'; if column is NULL, the search condition fails.
It is why you get told to avoid NULL columns whenever possible.
NULL <> 1 evaluates (theoretically) to "maybe", which means the record will not be returned.