Computer >> Computer tutorials >  >> Programming >> MySQL

How MySQL LTRIM() and RTRIM()functions can be used with WHERE clause?


MySQL LTRIM() and RTRIM() functions can be used to eradicate the leading and trailing spaces from the values of a column in a table. These functions work well even if we apply a condition in WHERE clause.

Example

mysql> Select LTRIM(Name) from Student;
+-------------+
| LTRIM(Name) |
+-------------+
| Gaurav      |
| Aarav       |
| Harshit     |
| Gaurav      |
| Yashraj     |
+-------------+
5 rows in set (0.13 sec)

The query above removes the leading space characters from the values of column ‘Name’.

mysql> Select RTRIM(Name) from Student;
+-------------+
| RTRIM(Name) |
+-------------+
| Gaurav      |
| Aarav       |
| Harshit     |
| Gaurav      |
| Yashraj     |
+-------------+
5 rows in set (0.00 sec)

The query above removes the trailing space characters from the values of column ‘Name’.

mysql> Select LTRIM(Name) from Student Where Name = 'Harshit';
+-------------+
| LTRIM(Name) |
+-------------+
| Harshit     |
+-------------+
1 row in set (0.05 sec)

The query above removes the leading space characters from the value i.e. given in WHERE clause ‘Harshit’, of column ‘Name’.

mysql> Select RTRIM(Name) from Student Where Name = 'Gaurav';
+-------------+
| RTRIM(Name) |
+-------------+
| Gaurav      |
| Gaurav      |
+-------------+
2 rows in set (0.00 sec)

The query above removes the trailing space characters from the value i.e. given in WHERE clause ‘Gaurav’, of column ‘Name’.