Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL


For this, you can use IS NOT NULL property. Let us first create a table −

mysql> create table DemoTable1
     (
     DueDate date
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1 values('2019-09-10');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1 values(NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1 values('2019-11-10');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1;

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 2019-09-10 |
| NULL       |
| 2019-11-10 |
+------------+
3 rows in set (0.00 sec)

Here is the query to create second table −

mysql> create table DemoTable2
     (
     AdmissionDate date
     );
Query OK, 0 rows affected (0.00 sec)

Here is the query to display only NOT NULL values −

mysql> insert into DemoTable2(AdmissionDate)
     select DueDate from DemoTable1 where DueDate IS NOT NULL;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Display all records from the table using select statement −

mysql> select * from DemoTable2;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2019-09-10    |
| 2019-11-10    |
+---------------+
2 rows in set (0.00 sec)

Updated on: 24-Dec-2019

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements