In what cases, we cannot use MySQL TRIM() function?

MySQLMySQLi Database

Actually for using MySQL TRIM() function we must have to know the string which we want to trim from the original string. This becomes the major drawback of TRIM() in the cases where we want to trim the strings having different values. For example, suppose we want to get the output after trimming the last two characters from the strings but every string is having different characters at last two places.

Example

mysql> Select * from Employee;

+------+----------------+------------+-----------------+
| Id   | Name           | Address    | Department      |
+------+----------------+------------+-----------------+
| 100  | Raman          | Delhi      | IT              |
| 101  | Mohan          | Haryana    | History         |
| 102  | Shyam          | Chandigarh | ENGLISH         |
| 103  | Sukhjeet Singh | Patiala    | Computer Engg.  |
| 104  | Bimal Roy      | Calcutta   | Computer Engg.  |
+------+----------------+------------+-----------------+

5 rows in set (0.00 sec)

As in the above table suppose I want to get the output after trimming the last two characters from Name, Address and Department Column then to get the desired output with one query by using TRIM() is not possible because the values at last two characters are not same.

Another case, when we cannot use TRIM() function, is When we want to remove the unwanted characters in the middle of the string. It is because TRIM() function only removes the unwanted leading or trailing characters from a string. For example, in the above table we cannot remove space between ‘Sukhjeet Singh’ by using TRIM() function.

raja
Published on 20-Feb-2018 10:02:06
Advertisements