How to extract substring from a sting starting at a particular position in MySQL?


For this, you can use the mid() function. Following is the syntax −

select mid(yourColumnName, yourPositionToStart, yourEndValue) as anyAliasName from yourTableName;

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('My best framework is Spring and Hibernate');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| My best framework is Spring and Hibernate |
+-------------------------------------------+
1 row in set (0.00 sec)

Here is the query to get part of a string in MySQL. We are trying to get 30 characters beginning from character 21. If the count of characters after 21 are not 30, then only the remaining characters will get displayed −

mysql> select mid(Title,21,30) as partofString from DemoTable;

Output

+-----------------------+
| partofString          |
+-----------------------+
| Spring and Hibernate  |
+-----------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements