Syntax error MySQL query to display only the column values with corresponding column having whitespace

MySQL query to display only the column values with corresponding column having whitespace



For this, use the TRIM() function. Let us first create a table −

mysql> create table DemoTable826(
   FirstName varchar(100),
   Age int
);
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using the insert command. Here, we have set the FirstName as whitespace for some of the records −

mysql> insert into DemoTable826 values(' ',24);
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable826 values(' ',22);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable826 values('Chris',26);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable826 values('Robert',27);
Query OK, 1 row affected (0.07 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable826 ;

This will produce the following output −

+-----------+------+
| FirstName | Age  |
+-----------+------+
| Adam      | 21   |
|           | 24   |
|           | 22   |
| Chris     | 26   |
| Robert    | 27   |
+-----------+------+
5 rows in set (0.00 sec)

Following is the query to display only the column values with the corresponding column having whitespace −

mysql> select *from DemoTable826 where COALESCE(TRIM(FirstName), '') = '';

This will produce the following output −

+-----------+------+
| FirstName | Age  |
+-----------+------+
|           | 24   |
|           | 22   |
+-----------+------+
2 rows in set (0.00 sec)
Updated on: 2019-09-03T12:19:19+05:30

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements