Get First N Characters from a MySQL Column



Use SUBSTRING() to get first N characters from a MySQL column. Let us first create a table −

mysql>create table DemoTable
(
   Information text
);
Query OK, 0 rows affected (2.63 sec)

Insert records in the table using insert command −

mysql>insert into DemoTable values('MySQL is a structured query language');
Query OK, 1 row affected (0.13 sec)

Following is the query to display all records from the table using select statement −

mysql>select *from DemoTable;

This will produce the following output −

+--------------------------------------+
| Information                          |
+--------------------------------------+
| MySQL is a structured query language |
+--------------------------------------+
1 row in set (0.00 sec)

Here is the query to get first N characters from the column. In this case, we are selecting the first 10 characters −

mysql>select substring(Information,1,10) from DemoTable;

This will produce the following output −

+-----------------------------+
| substring(Information,1,10) |
+-----------------------------+
| MySQL is a                  |
+-----------------------------+
1 row in set (0.00 sec)
Updated on: 2019-07-30T22:30:25+05:30

923 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements