Display custom text in a new column on the basis of null values in MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1953
   (
   StudentName varchar(20)
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1953 values('Chris');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1953 values(NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1953 values('David');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1953 values(NULL);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1953;

This will produce the following output −

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

Following is the query to display custom text in a new column on the basis of null values −

mysql> select StudentName,IF(StudentName IS NULL,'Student Name Is Not Found','Student Name Found') as Name from DemoTable1953;

This will produce the following output −

+-------------+---------------------------+
| StudentName | Name                      |
+-------------+---------------------------+
| Chris       | Student Name Found        |
| NULL        | Student Name Is Not Found |
| David       | Student Name Found        |
| NULL        | Student Name Is Not Found |
+-------------+---------------------------+
4 rows in set (0.00 sec)
raja
Published on 31-Dec-2019 06:54:49
Advertisements