Use MySQL REGEXP to ignore number and get only String and '/'

MySQLMySQLi Database

For this, use REGEXP_REPLACE(). Let us first create a table −

mysql> create table DemoTable1595
   -> (
   -> StudentCode varchar(50)
   -> );
Query OK, 0 rows affected (0.44 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1595 values('200 John');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1595 values('101 Carol/400 Taylor');
Query OK, 1 row affected (0.72 sec)
mysql> insert into DemoTable1595 values('101 302 405 Sam/9870');
Query OK, 1 row affected (0.28 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1595;

This will produce the following output −

+----------------------+
| StudentCode          |
+----------------------+
| 200 John             |
| 101 Carol/400 Taylor |
| 101 302 405 Sam/9870 |
+----------------------+
3 rows in set (0.00 sec)

Here is the query to use MySQL REGEXP to ignore number and get only String and '/' −

mysql> select regexp_replace(StudentCode,' *[0-9]+([.,][0-9]+)?(/[0-9]+([.,][0-9]+)?)? *', '') as `GetOnlyStringand/` from DemoTable1595;

This will produce the following output −

+-------------------+
| GetOnlyStringand/ |
+-------------------+
| John              |
| Carol/Taylor      |
| Sam/              |
+-------------------+
3 rows in set (0.00 sec)
raja
Published on 16-Dec-2019 07:13:35
Advertisements