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)