
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to check if field is null or empty in MySQL?
To check whether a field is null or empty in MySQL, use the IF() function in MySQL. The syntax is as follows −
SELECT IF(yourColumnName IS NULL or yourColumnName = '', 'NULLId', yourColumnName) as anyVariableName from yourTableName;
To understand the above syntax, let us create a table. The following is the query to create a table −
mysql> create table NullAndEmptyDemo -1> ( -> Id varchar(200) -> ); Query OK, 0 rows affected (0.66 sec)
Let us now insert records into the table with the help of insert command. The query to insert records in the table is as follows. We have added null and empty values as well −
mysql> insert into NullAndEmptyDemo values(NULL); Query OK, 1 row affected (0.22 sec) mysql> insert into NullAndEmptyDemo values('Smith123'); Query OK, 1 row affected (0.17 sec) mysql> insert into NullAndEmptyDemo values(' '); Query OK, 1 row affected (0.35 sec) mysql> insert into NullAndEmptyDemo values('98765'); Query OK, 1 row affected (0.15 sec) mysql> insert into NullAndEmptyDemo values(NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into NullAndEmptyDemo values('Carol567'); Query OK, 1 row affected (0.14 sec) mysql> insert into NullAndEmptyDemo values(''); Query OK, 1 row affected (0.13 sec)
Displaying all records with the help of select statement. The query is as follows −
mysql> select *from NullAndEmptyDemo;
The following is the output −
+----------+ | Id | +----------+ | NULL | | Smith123 | | | | 98765 | | NULL | | Carol567 | | | +----------+ 7 rows in set (0.00 sec)
Now you can apply the above syntax we discussed in the beginning to check whether the field is null or empty. The query is as follows −
mysql> SELECT IF(Id IS NULL or Id = '', 'NULLId', Id) as UpdatedColumnValue from NullAndEmptyDemo;
The following is the output that replace the values, if NULL or empty (“”) is found at first −
+--------------------+ | UpdatedColumnValue | +--------------------+ | NULLId | | Smith123 | | NULLId | | 98765 | | NULLId | | Carol567 | | NULLId | +--------------------+ 7 rows in set (0.00 sec)
- Related Articles
- Check whether a field is empty or null in MySQL?
- How do I check if a column is empty or null in MySQL?
- Check if a String is empty ("") or null in Java
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- How to check if a text field is empty or not in swift?
- Check if a String is whitespace, empty ("") or null in Java
- MySQL query to check if database is empty or not?
- Check for NULL or empty variable in a MySQL stored procedure
- How to check if data is NULL in MySQL?
- How to test String is null or empty?
- Only update the MySQL field if the field contains null or 0?
- Which one is better in MySQL - NULL or empty string?
- How to check if a field in MongoDB is [] or {}?
- Check if a table is empty or not in MySQL using EXISTS

Advertisements