
- 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
Substring() for Fields in MySQL?
You can use substring() for fields in MySQL to get part of a 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 some 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() 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
- How to apply Substring() for fields in MySQL to get part of string?
- Query in MySQL for string fields with a specific length?
- MySQL group_concat to add a separator for empty fields?
- How to concatenate fields in MySQL?
- MySQL find/ replace string in fields?
- Get number of fields in MySQL table?
- How to mask data fields in MySQL?
- Get the fields in each constraint in MySQL
- How can MySQL SUBSTRING() function be used with FROM and FOR keywords?
- How to ORDER BY grouped fields in MySQL?
- Display all fields of a table in MySQL?
- Fetch the substring after last dot in MySQL
- Get the substring of a column in MySQL
- MongoDB query for fields in embedded document?
- Biggest value from two or more fields in MySQL?

Advertisements