![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements