
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- Substring() for Fields in MySQL?
- How to get substring of a string in jQuery?
- Get the index of last substring in a given string in MySQL?
- How to order by certain part of a string in MySQL?
- Get part of a string based on a character in MySQL?
- Get number of fields in MySQL table?
- How to get substring in TypeScript?
- How to get a part of string after a specified character in JavaScript?
- How to cut part of a string with a MySQL query?
- MySQL query to get a substring from a string except the last three characters?
- Query in MySQL for string fields with a specific length?
- How to concatenate fields in MySQL?
- How to extract the digit part from the string in MySQL?
- PHP – How to get the selected part of a string using mb_substr()?
- How to check if string or a substring of string starts with substring in Python?

Advertisements