 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements
 