MySQL query to split the string “Learn With Ease” and return the last word?


For this, you can use SUBSTRING_INDEX(). Let us first create a table −

mysql> create table DemoTable
(
   Words TEXT
);
Query OK, 0 rows affected (1.62 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Learn With Ease');
Query OK, 1 row affected (0.32 sec)
mysql> insert into DemoTable values('Intro To MySQL');
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable values('Deep Dive Using Java');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('C++ In Depth');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------------------+
| Words                 |
+-----------------------+
| Learn With Ease       |
| Intro To MySQL        |
| Deep Dive Using Java  |
| C++ In Depth          |
+-----------------------+
4 rows in set (0.00 sec)

Following is the query to split the string “Learn With Ease” and return the last word −

mysql> update DemoTable set Words=substring_index(Words,' ',-1);
Query OK, 4 rows affected (0.28 sec)
Rows matched : 4 Changed : 4 Warnings : 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| Words    |
+----------+
| Ease     |
| MySQL    |
| Java     |
| Depth    |
+----------+
4 rows in set (0.00 sec)

Updated on: 10-Oct-2019

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements