MySQL query to remove string from a column with values EMP1, EMP2, EMP3, etc.


To remove string from the values EMO1, EMP2, etc., you need to use RIGHT() along with LENGTH(). Let us first create a table −

mysql> create table DemoTable1540
   -> (
   -> EmployeeCode varchar(20)
   -> );
Query OK, 0 rows affected (0.39 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1540 values('EMP9');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1540 values('EMP4');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1540 values('EMP8');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable1540 values('EMP6');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1540;

This will produce the following output −

+--------------+
| EmployeeCode |
+--------------+
| EMP9         |
| EMP4         |
| EMP8         |
| EMP6         |
+--------------+
4 rows in set (0.00 sec)

Here is the query to remove string from column values −

mysql> select right(EmployeeCode,length(EmployeeCode)-3) as onlyDigit from DemoTable1540;

This will produce the following output −

+-----------+
| onlyDigit |
+-----------+
| 9         |
| 4         |
| 8         |
| 6         |
+-----------+
4 rows in set (0.00 sec)

Updated on: 12-Dec-2019

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements