Check for NULL or NOT NULL values in a column in MySQL

MySQLMySQLi Database

For this, use IS NOT NULL in MySQL. Let us see the syntax−

select yourColumnName IS NOT NULL from yourTableName;

The above query returns 1 if the column does not have NULL value otherwise 0. Let us first create a −

mysql> create table DemoTable1408
   -> (
   -> FirstName varchar(30)
   -> );
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1408 values('Chris');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1408 values('');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1408 values(NULL);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1408 values('David');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select −

mysql> select * from DemoTable1408;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Chris     |
|           |
| NULL      |
| David     |
+-----------+
4 rows in set (0.00 sec)

Following is the query to check for NULL or NOT NULL −

mysql> select FirstName IS NOT NULL from DemoTable1408;

This will produce the following output −

+-----------------------+
| FirstName IS NOT NULL |
+-----------------------+
|                     1 |
|                     1 |
|                     0 |
|                     1 |
+-----------------------+
4 rows in set (0.00 sec)
raja
Published on 11-Nov-2019 11:05:03
Advertisements