Return similar names from different columns with distinct space allocations in MySQL?


For this, use LTRIM() and RTRIM(), since the names are similar but have different left and right space allocations. For example, “ John “ and “John”.

Let us first create a table −

mysql> create table DemoTable614 (EnterName varchar(100),ReennterName varchar(100));
Query OK, 0 rows affected (0.97 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable614 values(' John ','John');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable614 values('Carol ','Bob');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable614 values('David ','David');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable614;

This will produce the following output −

+------------+--------------+
| EnterName  | ReennterName |
+------------+--------------+
| John       | John         |
| Carol      | Bob          |
| David      | David        |
+------------+--------------+
3 rows in set (0.00 sec)

Following is the query to return similar names from different columns with distinct space allocations −

mysql> select *from DemoTable614 where LTRIM(RTRIM(EnterName)) = LTRIM(RTRIM(ReennterName));

This will produce the following output −

+------------+--------------+
| EnterName  | ReennterName |
+------------+--------------+
| John       | John         |
| David      | David        |
+------------+--------------+
2 rows in set (0.00 sec)

Updated on: 22-Aug-2019

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements