Need help selecting non-empty column values from MySQL
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) > ''
Related videos on Youtube
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, 2020Comments
-
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 over 11 yearsThis worked really well for me - especially the TRIM() version. Thanks.
-
Dante over 5 yearsHow can we use this in query instead of rawquery (android sqliteopenhelper)?
-
aravind_reddy about 5 yearsSELECT * FROM
table
WHERE column = "" worked for me