Trouble comparing smallint in where clause - sql

I have a table with a column called status of type smallint and accepts null. Sql server 2000.
My data contains mostly 2 in that field, but also 0 and null.
When I do
select *
from table
where status <> 2
I don't get all the proper records (where status is null or 0). Any idea why this is happening and how to correct? Shouldn't <> give me everything other than 2?

The NULL test doesn't match any arithmetic comparison.
use
where status <> 2 OR status is null

select *
from table
where ISNULL(status, 1) <> 2
NULL doesn't compare: so remove it.
Your example is the "common mistake" on Wikipedia too...

What you're expecting can be accomplished by setting ANSI_NULLS to off.
For example, try running these queries:
set ansi_nulls off
select case when 1 != null then 'true' else 'false' end
set ansi_nulls on
select case when 1 != null then 'true' else 'false' end
That being said, this is very non-standard SQL behaviour that you're expecting to see. NULL comparisons should always be considered false whether equals to or not equals to comparisons, as every developer will expect that type of SQL query behaviour.
Your WHERE clause should then look like:
where status <> 2 or status is null
Another option would be just to compare to status = 1, if that is the only status that you're expecting to be included in your query.

Related

how to check if existing column changes from some value to NULL using Oracle

I'd like to check the following condition using sql query using Oracle:
to check id a user changes Condition from some value to another value
to check if a user changes condition from some value to NULL
I am trying for the below query:
(select count(*) from MyTable where (:userconditionid = (select condition_id from MyTable
where item = :useritem))
and item = :useritem)
where :userconditioncode is the value user is trying to enter value, the above query works for normal values but it wont work for null value , if user changes the condition from some value to null value
the above sql query fails.
Sample Data and Desired Results
If user is trying to alter the condition_id to NULL
then I have to throw am error as follows:
"User are not allowed to change the value to null"
also if user is trying to alter the condition from NULL to some other value
then it should not throw an error.
If I correctly got all your comments - the only case to throw error for you is when not-null value is tried to be changed to null. Consider using
select case when condition_id is not null and :userconditionid is null then 1 else 0 end as do_throw_error
from MyTable
where item = :useritem
in this case.
The thing about nulls is that almost all '=' comparisons to null return false.
The proper test is not '=' but 'is null':
In P/L SQL:
if :userConditionCode is null then
throw...
or is SQL:
(select count(*) from MyTable
where
(
(:userconditionid = (select condition_id from MyTable
where item = :useritem)
)
or :userconditionid is null
)
and item = :useritem)
The only way to check the value for null in SQL is IS NULL predicate. Of course, you can translate null to something else but real NULL check is nothing else than IS NULL.
Does this code suits your need? You've not wrote what should be done when there's no data for requested :useritem.
select 'User are not allowed to change the value to null' as msg
from MyTable
where item = :useritem
having (
min(1) /*Table has that item*/,
min(case
when f.condition_id is not null and :userconditionid is null
then 1
end) /*User sets non null value to null*/
) in ((1,1))

<> and != avoiding NULL in WHERE condition

I have a table PATIENT with Column STATUS. When I queried to get STATUS not equal to 1, I was expecting the result as NULL and 2.
But I am only getting 2 as the result. Can someone help me with this?
CREATE TABLE #PATIENT
(STATUS INT)
INSERT INTO #PATIENT (STATUS)
SELECT 1
UNION
SELECT 2
UNION
SELECT NULL
SELECT * FROM #PATIENT WHERE STATUS <> 1
When I queried with
SELECT * FROM #PATIENT WHERE ISNULL(STATUS, 0) != 1
I am able to get NULL and 2 as the result.
This is SQL SERVER 2012.
You can use OR in WHERE with Condition STATUS IS NULL .
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
This will do it.
EDIT:
Conceptually, NULL means “a missing unknown value” and it is
treated somewhat differently from other values.
You cannot use arithmetic comparison operators such as =, <, or <>
to test for NULL
Because the result of any arithmetic comparison with NULL is also
NULL, you cannot obtain any meaningful results from such comparisons
we can not equate or not equate anything with null, thats why IS NULL
SELECT NULL <> 1 ===> NULL
Even though it is supposed to be true, it will return `NULL`
Hope this helps.
When you compare NULL value to any value then the result is always NULL.
So if you wan to select the NULL value as well then try this:
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
DEMO
Conceptually "NULL" means a missing value. To test for NULL IS NULL or IS NOT NULL condition is used. Arithmetic operators cannot be used for comparing NULL values.
NULL<>1 :: returns false because NULL can be 1 or any other value (an unknown value).
Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database.
And reason because you got only 2 and NOT NULL is because in SQL nothing is equal to NULL but also nothing is NOT equal to NULL.
In Simple words you cannot equate and not equate any value with NULL.

Null value in column resulting into no output in sql query

I have 2 tables (stud and stud1). Both having 2 columns but stud1 contains 1 record which is null.
I have created following 2 queries.First one is returning the accurate result but other one which is using not in returning nothing. I guess that is because of the null value. But I don't understand the reason for it. Can someone help me with this?
See NOT IN clause and NULL values.
That's because your 2nd query equals:
SELECT * FROM #stud
WHERE ID <> NULL
When ansi_nulls is on, ID <> NULL is unknown, so you won't get any rows.

Checking tables for null returns 0 regardless

I don't understand why this query doesn't work.
I have a table which is full of rows where 3 of the column values are set to NULL.
But when I run the following query, it returns 0 (it should return 96)
SELECT COUNT(*) FROM SEAT WHERE BOOKED=null;
Do you know what I am doing wrong?
It depends on your database settings and the specific RDBMS you are using, but if you are using ANSI NULL syntax you cannot directly compare a value to NULL with an equality test (=) -- it will always fail.
Use WHERE BOOKED IS NULL instead.
You have to use IS NULL instead of = null
Since null technically isn't a value, you can't compare null using the = operator.
Use IS NULL
SELECT COUNT(*)
FROM SEAT
WHERE BOOKED IS NULL

SQL 'select' Query with 3 conditions

SELECT *
FROM REVIEW
WHERE REVIEWERID =5 AND APPRAISEECONFIRMYN='Y' AND HRCONFIRMYN = NULL
Are 2 'AND' conditions allowed like this? I'm not getting the correct output. There are 2 records in the database that fulfil the above conditions. When I remove the last condition 'HRCONFIRMYN = NULL' and execute, I get the correct output.
How can this be solved? I need to check all 3 conditions whilst searchng the records.
To compare the NULL values, you have to use the IS NULL predicate instead of = NULL like so:
SELECT *
FROM REVIEW
WHERE REVIEWERID = 5
AND APPRAISEECONFIRMYN = 'Y'
AND HRCONFIRMYN IS NULL
use OR instead
group your condition
use IS NULL when comparing with NULLs
query,
SELECT *
FROM REVIEW
WHERE (REVIEWERID =5 AND APPRAISEECONFIRMYN='Y') OR
HRCONFIRMYN IS NULL