Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help selecting non-empty column values from MySQL

Tags:

sql

null

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.

like image 256
codacopia Avatar asked Sep 05 '25 17:09

codacopia


2 Answers

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) <> ''
like image 115
Michael Berkowski Avatar answered Sep 07 '25 17:09

Michael Berkowski


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

SELECT * FROM `table` WHERE TRIM(column) > ''
like image 45
user1368905 Avatar answered Sep 07 '25 17:09

user1368905