Need help selecting non-empty column values from MySQL

66,674

Solution 1

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

Solution 2

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

SELECT * FROM `table` WHERE TRIM(column) > ''
Share:
66,674

Related videos on Youtube

codacopia
Author by

codacopia

Codacopia is a website development company specializing in Wordpress development. Whether it is a quick fix and consultation needed or full site redesign, we've got ya covered. Are you looking to build your marketing efforts online? If so that's what we do best, so let's get in touch. Vist us at codacopia.com

Updated on July 15, 2020

Comments

  • codacopia
    codacopia almost 4 years

    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.

  • Chris
    Chris over 11 years
    This worked really well for me - especially the TRIM() version. Thanks.
  • Dante
    Dante over 5 years
    How can we use this in query instead of rawquery (android sqliteopenhelper)?
  • aravind_reddy
    aravind_reddy about 5 years
    SELECT * FROM table WHERE column = "" worked for me