Select non empty columns in MySQL?

Sometimes it’s required to troubleshoot or optimize MySQL database and we need to select non empty columns in MySQL. To Select non empty columns in MySQL and as a general our query is totally understandable. First, We need to write SELECT Query for MySQL. Afterward, We need * to check all columns, all data, we need to check in FROM some of MySQL TABLENAME. Finally, the column name, it must be IS NOT NULL. So, put all these conditions together and make a fairly simple MySQL Query.

SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IS NOT NULL

Select non empty columns

Sometimes, during your troubleshooting, you might face the situation when above MySQL query is not working and you in some scenarios you might dam sure that you are checking a MySQL table and its column that is non-empty. In this scenario, we mostly need to optimize our query (the best practice indeed) with TRIM () along with our above MySQL Query and we just need to put AND TRIM () at the end of our MySQL Query. Actually TRIM will show results even there is some space in the results and target column, Here its final look;

SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IS NOT NULL  AND TRIM (COLUMN_NAME)”

It’s the final query and best practice to use this query as we do not know that which row might contains some space and we can avoid facing the empty results from the MySQL query. Please make it sure that at the end of line, there is single quotes.

Share

You may also like...