Tutorialspoint

1 Answer
Rama Giri

Use ISNULL() from MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Number1 int,
   -> Number2 int
   -> );
Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10,NULL);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(NULL,NULL);
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable values(29,98);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values(NULL,119);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement.

mysql> select *from DemoTable;

This will produce the following output −

+---------+---------+
| Number1 | Number2 |
+---------+---------+
|      10 | NULL    |
|    NULL | NULL |
|      29 | 98    |
|    NULL | 119 |
+---------+---------+
4 rows in set (0.00 sec)

Following is the query to count the number of NULLs in a row.

mysql> select Number1,Number2,isnull(Number1)+isnull(Number2) AS NumberofNULLS from DemoTable;

This will produce the following output −

+---------+---------+--------------+
| Number1 | Number2 | NumberofNULLS |
+---------+---------+--------------+
|      10 | NULL |            1 |
|    NULL | NULL |         2 |
|      29 | 98 |       0 |
|    NULL | 119 |       1 |
+---------+---------+--------------+
4 rows in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.