Search code examples
mysqlsqlnull

Need help selecting non-empty column values from MySQL


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.


Solution

  • 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) <> ''