Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select rows where field is not blank/empty

Tags:

mysql

I'm trying to do a MySQL query in phpMyAdmin. I want to find an entry where fieldone is not NULL or is not empty/blank as I inherited the system so fields are set to NULL and some are just blank.

Anyways, Here is the query I'm trying

SELECT fieldone, fieldtwo
FROM tableone
WHERE fieldone != ' '
OR fieldone IS NOT NULL

And

SELECT fieldone, fieldtwo
FROM tableone
WHERE fieldone <> ' '
OR fieldone IS NOT NULL

Both show an error #1064 on the line that contains

WHERE fieldone != ' '

And

WHERE fieldone <> ' '

The NOT NULL part works great, its just trying to find any fields that are blank.

like image 708
snookian Avatar asked Apr 26 '13 09:04

snookian


2 Answers

SELECT fieldone, fieldtwo
FROM tableone
WHERE fieldone != ''
OR fieldone IS NOT NULL

SELECT fieldone, fieldtwo
FROM tableone
WHERE fieldone <> ''
OR fieldone IS NOT NULL

When you mean NULL value then don't include a space between your '' so it means NO CONTENT

like image 135
OmniPotens Avatar answered Nov 15 '22 05:11

OmniPotens


You could try this:

SELECT fieldone, fieldtwo 
FROM tableone 
WHERE fieldone IS NOT NULL 
AND TRIM(fieldone) <> ''
like image 23
Lordicon Avatar answered Nov 15 '22 06:11

Lordicon