Why Select eliminating NULL records on a varchar comparison - sql

I have a table with 782,856 records. There is a column PEOPLE_TYPE in this table that is varchar(20). I don't think table schema matters but if it does I will gladly post it.
It has these distinct values (parens is a count of each type):
NULL (782,101)
ANONYMOUS (1)
BOARD (530)
USER (224)
So why does this select return these results???
select * from people where PEOPLE_TYPE != 'BOARD'
This return 225 rows...USER & ANONYMOUS....why aren't my nulls included...because I have now performed a text search and NULLs can't really be compared so they are eliminated?
Thank You for your patience with my remedial question.

NULL is a strange thing. Any comparison with NULL is false:
NULL = NULL is false
NULL != anything is false
NULL != NULL is also false.
You have to say things like column is null, or column is not null.
Your query would need
select * from people where PEOPLE_TYPE != 'BOARD' or PEOPLE_TYPE is null

You can read this for details on why records with NULL are not being returned: http://msdn.microsoft.com/en-us/library/ms188048.aspx
If you want records with NULL to be returned you need to write the query like this:
select * from people where ISNULL(PEOPLE_TYPE, '0') != 'BOARD'
Or this:
select * from people where PEOPLE_TYPE != 'BOARD' OR PEOPLE_TYPE IS NULL

There is another thing called COALESCE, http://msdn.microsoft.com/en-us/library/ms190349.aspx
I use quite often,
SELECT * FROM People WHERE COALESCE(PEOPLE_TYPE, '') != 'BOARD'

Related

<> 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.

Returning varchars that are not null or empty SQL

I have a column in SQL that is varchar. I need it to return anything with a value.
Example...
select * from students where StudentID <> ''
Is this the correct way of doing it? I've tried is not null but then it returns anything that is empty as well.
Thanks
I would suggest using coalesce:
select * from students where coalesce(StudentID, '') <> ''
This will turn nulls into empty strings and disallow them. This has the added bonus of restricting empty strings as well.
A null is not equal to anything, not even another null, so a simple <> doesnt work.
select * from students where StudentID <> '' AND StudentID IS NOT NULL
You can target both white space and null.
There's something call NOT NULL
SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL
Is that helping ?
try this:
select * from students where StudentID is not null
You have to handle the case that it's not null separately because you cannot compare with null-values. They are neither equal nor unequal to any other value(incl. null). NULL means "unknown value", so any comparison with any actual value makes no sense
....
WHERE StudentID IS NOT NULL AND StudentID <> ''
You could use ISNULL(StudentID,'') <> '' (or COALESCE). But i think this is more efficient.
Try using ISNULL()
select * from student where isnull(studentid,'') <> ''

Rule out blank and null values in WHERE clause

Many times I have to run this query:
select * from users where name is not null and name != ''
Is there any better way to do this. I need more performance, any suggestion. I guess this is very common, so there may be some compiled function which will be something like
select * from users where name is present()
Using PostgreSQL 9 version.
For any x, null != x will be null and null is not true. That means that you can simply ignore NULLs in your case and say:
select * from users where name != ''
You could also convert NULLs to empty strings using COALESCE if that's clearer to you:
select * from users where coalesce(name, '') != ''
Of course that coalesce call isn't free.
Demo: http://sqlfiddle.com/#!2/e810c/2
You can use NULLIF:
SELECT *
FROM USERS
WHERE NULLIF(NAME,'') IS NOT NULL

if condition in where statement

How do I write a query that if one column value is not null then compare the value with the querystring, else do not compare the column value.
for example, I have a tbl1 with programs1 and programs2 two columns, the value in these two columns can be null or with value. I want to have the if statement that say
if programs1 is not null, programs1=#programs and
if programs2 is not null, programs2=#programs
my query is like this:
select * from tbl1 where
(programs1 is not null and programs1=#programs)
and
(program2 is not null and programs2=#programs)
but it didn't work the way I want. How do I write a correct query for this case? TIA.
I believe you're looking for COALESCE.
WHERE COALESCE(programs1, programs2) = #Programs
However, if you'd like to compare only if it has a value:
-- If the column is null, ignore it. If it has a value, then
-- check that value.
WHERE (programs1 IS NULL OR programs1 = #Programs)
AND (programs2 IS NULL OR programs2 = #Programs)
Also, I assume you mean sql-server since you reference asp.net
ISNULL(programs1,programs2) = #Programs
or
(programs1 is not null and #Programs = programs1) or (programs2 is not null and #Programs = programs2)
select *
from tbl1
where (programs1=#programs or programs1 is null)
and (programs2=#programs or programs2 is null)

Select rows where column is null

How do you write a SELECT statement that only returns rows where the value for a certain column is null?
Do you mean something like:
SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL
?
I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:
SELECT * FROM customers WHERE first_name IS NULL
On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:
SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
Other DBMSes have similar functionality available.
If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.
Use Is Null
select * from tblName where clmnName is null
You want to know if the column is null
select * from foo where bar is null
If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it
does not work:
select * from foo where bar <> 'value'
does work:
select * from foo where bar <> 'value' or bar is null
in Oracle (don't know on other DBMS) some people use this
select * from foo where NVL(bar,'n/a') <> 'value'
if I read the answer from tdammers correctly then in MS SQL Server this is like that
select * from foo where ISNULL(bar,'n/a') <> 'value'
in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.
select Column from Table where Column is null;
select * from tableName where columnName is null
For some reasons IS NULL may not work with some column data type. I was in need to get all the employees that their English full name is missing, I've used:
SELECT emp_id, Full_Name_Ar, Full_Name_En
FROM employees
WHERE Full_Name_En = '' or Full_Name_En is null