MySQL query to get a field value that does not contain empty spaces?


Use NOT LIKE for this. Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentFullName varchar(40)
   );
Query OK, 0 rows affected (0.66 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable(StudentFullName) values('JohnSmith');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(StudentFullName) values('John Doe');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(StudentFullName) values('Adam Smith');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(StudentFullName) values('CarolTaylor');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+----+-----------------+
| Id | StudentFullName |
+----+-----------------+
| 1  | JohnSmith       |
| 2  | John Doe        |
| 3  | Adam Smith      |
| 4  | CarolTaylor     |
+----+-----------------+
4 rows in set (0.00 sec)

Following is the query to get a field value that does not contain empty spaces −

mysql> select *from DemoTable where StudentFullName NOT LIKE '% %';

This will produce the following output −

+----+-----------------+
| Id | StudentFullName |
+----+-----------------+
| 1  | JohnSmith       |
| 4  | CarolTaylor     |
+----+-----------------+
2 rows in set (0.18 sec)

Updated on: 30-Jul-2019

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements