
- 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 data is NULL in MySQL?
You can use IF() to check if data is NULL. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(200), Age int ); Query OK, 0 rows affected (0.44 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(Name,Age) values('John',23); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name,Age) values('Sam',null); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name,Age) values('Mike',23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Name,Age) values('David',21); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name,Age) values('Carol',null); Query OK, 1 row affected (0.13 sec)
Display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 23 | | 2 | Sam | NULL | | 3 | Mike | 23 | | 4 | David | 21 | | 5 | Carol | NULL | +----+-------+------+ 5 rows in set (0.00 sec)
Here is the query to check if data is NULL or not. This adds a message wherever NULL in the record is visible −
mysql> select if(Age IS NULL,'Age is missing',Age) from DemoTable;
This will produce the following output −
+--------------------------------------+ | if(Age IS NULL,'Age is missing',Age) | +--------------------------------------+ | 23 | | Age is missing | | 23 | | 21 | | Age is missing | +--------------------------------------+ 5 rows in set (0.00 sec)
- Related Articles
- How to check if field is null or empty in MySQL?
- In MySQL stored procedures, how to check if a local variable is null?
- How to check if any value is Null in a MySQL table single row?
- How do I check if a column is empty or null in MySQL?
- How to check if a variable is NULL in C/C++?
- Update one column data to another column in MySQL if the second column is NOT NULL?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- Insert default into not null column if value is null in MySQL?
- Check whether a field is empty or null in MySQL?
- How to check if a timestamp is set in MySQL?
- How can we check for NULL in a MySQL query?
- Check if a String is empty ("") or null in Java
- Check for NULL or NOT NULL values in a column in MySQL
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null

Advertisements