For this, you can use substring() along with length(). Let us first create a table −
mysql> create table DemoTable1329 -> ( -> StudentName varchar(40) -> ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1329 values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1329 values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1329 values('Adam Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1329 values('John Doe'); Query OK, 1 row affected (0.44 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1329;
This will produce the following output −
+--------------+ | StudentName | +--------------+ | David Miller | | Chris Brown | | Adam Smith | | John Doe | +--------------+ 4 rows in set (0.00 sec)
Here is the query to trim the X number of characters, beginning from the last −
mysql> select substring(StudentName, 1, length(StudentName) - 5) from DemoTable1329;
This will produce the following output −
+----------------------------------------------------+ | substring(StudentName, 1, length(StudentName) - 5) | +----------------------------------------------------+ | David M | | Chris | | Adam | | Joh | +----------------------------------------------------+ 4 rows in set (0.00 sec)