Replace NULL values

MySQL Best Practices

You can show the null values which represent the concept of missing or inapplicable information, with other values for better data representation.

Suppose we have a table as below:

NameCountry
JohnIndia
JaneAustralia
Tim[NULL]

So if you query from the database the null result will show the blank result.

You can replace NULL values by another value at run time.

SELECT name, 
       IF(country IS NULL,"No Country",country) country
FROM users;
NameCountry
JohnIndia
JaneAustralia
TimNo Country

MySQL also handles the null values directly using IFNULL function.

SELECT name, 
       IFNULL(country,"No Country") country
FROM users;

Leave a Reply

Your email address will not be published. Required fields are marked *