Split a column after hyphen in MySQL and display the remaining value?


To split a column after hyphen, use the SUBSTRING_INDEX() method −

select substring_index(yourColumnName,'-',-1) AS anyAliasName from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> StreetName text
   -> );
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Paris Hill St.-CA-83745646') ;
Query OK, 1 row affected (0.32 sec)

mysql> insert into DemoTable values('502 South Armstrong Street-9948443');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------------------------------+
| StreetName                         |
+------------------------------------+
| Paris Hill St.-CA-83745646         |
| 502 South Armstrong Street-9948443 |
+------------------------------------+
2 rows in set (0.00 sec)

Following is the query to split a column after specific characters −

mysql> select substring_index(StreetName,'-',-1) AS Split from DemoTable;

This will produce the following output −

+----------+
| Split    |
+----------+
| 83745646 |
| 9948443  |
+----------+
2 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

469 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements