Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter out the rows which contain a particular column with null or empty data in SQL?

Tags:

sql

In SQL, How we make a check to filter all row which contain a column data is null or empty ?
For examile

Select Name,Age from MEMBERS

We need a check Name should not equal to null or empty.

like image 683
Nakul Chaudhary Avatar asked Nov 28 '08 09:11

Nakul Chaudhary


People also ask

How do you find rows in which a particular column contains a null value?

Use the IS NULL operator in a condition with WHERE to find records with NULL in a column. Of course, you can also use any expression instead of a name of a column and check if it returns NULL. Nothing more than the name of a column and the IS NULL operator is needed (in our example, middle_name IS NULL ).

How do I filter a specific row in SQL?

You can use the WHERE clause to filter unwanted rows from the result. This filtering capability gives the SELECT statement its real power. In a WHERE clause, you specify a search condition that has one or more conditions that need to be satisfied by the rows of a table.


2 Answers

This will work in all sane databases (wink, wink) and will return the rows for which name is not null nor empty

select name,age from members where name is not null and name <> ''
like image 142
Vinko Vrsalovic Avatar answered Sep 21 '22 16:09

Vinko Vrsalovic


For DBMSs that treat '' as a value (not null), Vinko's query works:

select name,age from members where name is not null and name <> ''

For Oracle, which treats '' as a null, tha above doesn't work but this does:

select name,age from members where name is not null

I tried to think of a "DBMS-agnostic" solution, but failed - mainly due to the different concatenation operators/functions used by different DBMSs!

like image 24
Tony Andrews Avatar answered Sep 17 '22 16:09

Tony Andrews