126

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?

10 Answers 10

256

Either use

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

or use

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() or coalesce(field1, 'empty'). But that is not suitable for empty strings.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that if you want also to consider the strings as empty those with only spaces, you could use the TRIM function like SELECT IF(field1 IS NULL or TRIM(field1) = '', 'empty', field1) as field1 FROM tablename.
You should not add an alternative way when it is already given in other answers.
56

Using nullif does the trick:

SELECT ifnull(nullif(field1,''),'empty or null') AS field1
  FROM tablename;

How it works: nullif is returning NULL if field is an empty string, otherwise returns the field itself. This has both the cases covered (the case when field is NULL and the case when it's an empty string).

2 Comments

nullif(field, '') is usefull
This is very useful, but to be more clear, the second argument of ifnull() in the example should be 'empty or null'.
17

Alternatively you can also use CASE for the same:

SELECT CASE WHEN field1 IS NULL OR field1 = '' 
       THEN 'empty' 
       ELSE field1 END AS field1
FROM tablename.

Comments

12

You can use the IFNULL function inside the IF. This will be a little shorter, and there will be fewer repetitions of the field name.

SELECT IF(IFNULL(field1, '') = '', 'empty', field1) AS field1 
FROM tablename

Comments

9

You can create a function to make this easy.

create function IFEMPTY(s text, defaultValue text)
returns text deterministic
return if(s is null or s = '', defaultValue, s);

Using:

SELECT IFEMPTY(field1, 'empty') as field1 
from tablename

Comments

2

By trimming and comparing, we ensure we also take care of empty or tab or space character content in the field.

SELECT
    CASE
        WHEN LTRIM(RTRIM(col_1))='' or col_1 IS NULL THEN 'Not available'
        ELSE col_1
    END AS col_alias
FROM
    my_table

Comments

1

IF (with Mysql 8.x at least) works on truthy/falsy values:

IF(NULL, 'truthy', 'falsy')
IF('',   'truthy', 'falsy')
IF(0,    'truthy', 'falsy') -- beware
IF('0',  'truthy', 'falsy') -- beware

Every one of the above will return 'falsy'. So if you have a field that can be null or empty string (ie left join, etc):

IF(myField, 'not empty', 'empty')

This will work just fine exactly the way you'd think it would (given the caveats of 0 and '0'). Kind of infuriating considering comparing NULL values to NON-NULL values gives inconsistent results.

1 Comment

sorry, does not work, e.g. select IF('x', 'not empty', 'empty') gives 'empty'
0
SELECT * FROM ( 
    SELECT  2 AS RTYPE,V.ID AS VTYPE, DATE_FORMAT(ENTDT, ''%d-%m-%Y'')  AS ENTDT,V.NAME AS VOUCHERTYPE,VOUCHERNO,ROUND(IF((DR_CR)>0,(DR_CR),0),0) AS DR ,ROUND(IF((DR_CR)<0,(DR_CR)*-1,0),2) AS CR ,ROUND((dr_cr),2) AS BALAMT, IF(d.narr IS NULL OR d.narr='''',t.narration,d.narr) AS NARRATION 
    FROM trans_m AS t JOIN trans_dtl AS d ON(t.ID=d.TRANSID)
    JOIN acc_head L ON(D.ACC_ID=L.ID) 
    JOIN VOUCHERTYPE_M AS V ON(T.VOUCHERTYPE=V.ID)  
    WHERE T.CMPID=',COMPANYID,' AND  d.ACC_ID=',LEDGERID ,' AND t.entdt>=''',FROMDATE ,''' AND t.entdt<=''',TODATE ,''' ',VTYPE,'
    ORDER BY CAST(ENTDT AS DATE)) AS ta

1 Comment

here anybody can find there qry's solution.its a runing qry and hope it will b usefull for everyone
0
SELECT IF(TRIM(COALESCE(field1, '')) = '', '[ empty ]', field1) FROM tablename

field1 is null or any length of whitespace

See TRIM COALESCE and IS NULL for more info.

Also Working with null values from the MySQL docs

Comments

-1

If you would like to check in PHP , then you should do something like :

$query_s =mysql_query("SELECT YOURROWNAME from `YOURTABLENAME` where name = $name");
$ertom=mysql_fetch_array($query_s);
if ('' !== $ertom['YOURROWNAME']) {
  //do your action
  echo "It was filled";
} else { 
  echo "it was empty!";
}

1 Comment

$isnull = (empty($ertom['YOUROWNAME']) ? empty : not empty); That's a little shorter..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.