

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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 apply Substring() for fields in MySQL to get part of string?
You can use substring() for fields in MySQL to get part of string. Following is the syntax −
select substring(yourColumnName,yourStartingIndex,yourEndingIndex) from yourTableName;
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Title longtext ); Query OK, 0 rows affected (0.57 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(Title) values('MySQL is a relational database management system'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Title) values('MongoDB is a popular No SQL database'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+----+--------------------------------------------------+ | Id | Title | +----+--------------------------------------------------+ | 1 | MySQL is a relational database management system | | 2 | MongoDB is a popular No SQL database | +----+--------------------------------------------------+ 2 rows in set (0.00 sec)
Following is the query to use substring() method for fields −
mysql> select substring(Title,1,21) from DemoTable;
This will produce the following output −
+-----------------------+ | substring(Title,1,21) | +-----------------------+ | MySQL is a relational | | MongoDB is a popular | +-----------------------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- Substring() for Fields in MySQL?
- How to get substring of a string in jQuery?
- Get part of a string based on a character in MySQL?
- How to order by certain part of a string in MySQL?
- MySQL query to replace part of string before dot
- How to cut part of a string with a MySQL query?
- How to get a part of string after a specified character in JavaScript?
- Get number of fields in MySQL table?
- Get the index of last substring in a given string in MySQL?
- PHP – How to get the selected part of a string using mb_substr()?
- Replace part of string in MySQL table column?
- How to extract the digit part from the string in MySQL?
- Query in MySQL for string fields with a specific length?
- Get the substring of a column in MySQL
- How to replace a part of the string (domain name after @) using MySQL?
Advertisements