MySQL query to split a column after specific characters?



To split a column after specific characters, 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;

Output

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;

Output

This will produce the following output −

+----------+ | Split    | +----------+ | 83745646 | | 9948443  | +----------+ 2 rows in set (0.00 sec)
Updated on: 2020-06-30T12:53:12+05:30

964 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements