For this, you can use the property IS NULL for null values in MySQL. Let us first create a table −
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Name | +--------+ | Robert | | NULL | | David | | NULL | | Robert | +--------+ 5 rows in set (0.00 sec)
Following is the query to replace null values from the table −
mysql> update DemoTable set Name=IF(Name IS NULL,'Please Enter a Name',Name); Query OK, 2 rows affected (0.09 sec) Rows matched: 5 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | Name | +---------------------+ | Robert | | Please Enter a Name | | David | | Please Enter a Name | | Robert | +---------------------+ 5 rows in set (0.00 sec)