Rule out blank and null values in WHERE clause - sql

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

Related

DB2 SQL Statement WHERE clause CASE WHEN in multiple conditions

This is what I am trying to do:
SELECT
id, name
FROM
users
WHERE
isActive=true
(AND CASE WHEN {param} != null THEN name={param} ELSE null END)
if the passed {param} is not null then only the AND operator will be added otherwise just isActive=true condition will be used.
You can use something like COALESCE or (in case of DB2) NVL.
SELECT
id, name
FROM
users
WHERE
isActive=true
AND name=COALESCE({param},name)
You didn't say how you pass parameters/variables, so i'll leave it to you. "{param}" could be replaced with a column or constant in this example
try this
SELECT
id, name
FROM
users
WHERE
isActive=true
AND (({param} is not null AND name={param}) OR ({param} is 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,'') <> ''

Why Select eliminating NULL records on a varchar comparison

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'

mysql ifnull where

i need such query
select * from t where
field=ifnull(:param, field) 'it not work's
so if param=NULL i have
select * from t where field is NULL
but if param =4
i have
select * from t where field=4
You can use the case when in where clause AFAIK bot not sure about MySQl,
But the better approach is to translate them,
you can read about that SQL WHERE clauses: Avoid CASE, use Boolean logic
So
select * from t where (:param is null and filed is null) or (filed = :param)
You can try this alternative
this might help you
select * from t where (field = NULL AND param= NULL) OR field ='4'
When working with NULL you cannot use arithmetic operators. Try COALESCE to make a logical if with values integer or NULL
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL
I think you are looking for NULLIF instead of ifnull
I think better approach would be to use CASE in where clause in your case.

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