veereev veereev - 1 year ago 128
SQL Question

How to check if field is null or empty mysql?

I am trying to figure out how to check if a field is null or empty. I have this

SELECT IFNULL(field1, 'empty') as field1 from tablename.

I need to add an additional check field1 != "" something like

SELECT IFNULL(field1, 'empty') OR field1 != "" as field1 from tablename.

Any idea how to accomplish this?

Answer Source

Either use

SELECT IF(field1 IS NULL or field1 = '', 'empty', field1) as field1 
from tablename


SELECT case when field1 IS NULL or field1 = ''
            then 'empty'
            else field1
       end as field1 
from tablename

If you only want to check for null and not for empty strings then you can also use ifnull as you tried. But that is not suitable for empty strings too.