Implement two conditions for a single column in MySQL for null and empty value


Let us first create a table −

mysql> create table DemoTable635(
   EmployeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,EmployeeName varchar(100)
);
Query OK, 0 rows affected (1.24 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable635(EmployeeName) values('John');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable635(EmployeeName) values('Sam');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable635(EmployeeName) values('');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable635(EmployeeName) values(null);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable635(EmployeeName) values('David');
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable635;

This will produce the following output −

+-----------+--------------+
| EmployeId | EmployeeName |
+-----------+--------------+
|         1 | John         |
|         2 | Sam          |
|         3 |              |
|         4 | NULL         |
|         5 | David        |
+-----------+--------------+
5 rows in set (0.00 sec)

Following is the query to avoid displaying empty and null value from a MySQL column −

mysql> select *from DemoTable635 tbl where tbl.EmployeeName is not null and tbl.EmployeeName <> '';

This will produce the following output −

+-----------+--------------+
| EmployeId | EmployeeName |
+-----------+--------------+
|         1 | John         |
|         2 | Sam          |
|         5 | David        |
+-----------+--------------+
3 rows in set (0.02 sec)

Updated on: 23-Aug-2019

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements