30

I have a MySQL table which has about 30 columns. One column has empty values for the majority of the table. How can I use a MySQL command to filter out the items which do have values in the table?

Here is my attempt:

SELECT * FROM `table` WHERE column IS NOT NULL

This does not filter because I have empty cells rather that having NULL in the void cell.

3 Answers 3

64

Also look for the columns not equal to the empty string ''

SELECT * FROM `table` WHERE column IS NOT NULL AND column <> ''

If you have fields containing only whitespace which you consider empty, use TRIM() to eliminate the whitespace, and potentially leave the empty string ''

SELECT * FROM `table` WHERE column IS NOT NULL AND TRIM(column) <> ''
Sign up to request clarification or add additional context in comments.

3 Comments

This worked really well for me - especially the TRIM() version. Thanks.
How can we use this in query instead of rawquery (android sqliteopenhelper)?
SELECT * FROM table WHERE column = "" worked for me
12

A alternate approach that also handles blank spaces in a column as well as null:

SELECT * FROM `table` WHERE TRIM(column) > ''

Comments

0

Sometimes seems the value from a field is empty but the query filter ignores and still shows the result with empty fields. From my experience I recommend a trick:

SELECT * FROM `table` WHERE column IS NOT NULL AND CONCAT_WS(",",TRIM(column),TRIM(column)) <> ','

Very weird approach but works 100%!!

MySql says in documentation that

column IS NOT NULL AND TRIM(column) <> ''

will work but this does not happen always, trust me!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.