Return only the first 15 characters from a column with string values in MySQL

MySQLMySQLi Database

To return only the first 15 characters from string values, use the MySQL SUBSTR() function.

Let us first create a table −

mysql> create table DemoTable
(
   Title varchar(100)
);
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Introduction to Java');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('C in Depth with data structure and algorithm');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------------------------------------+
| Title                                        |
+----------------------------------------------+
| Introduction to MySQL                        |
| Introduction to Java                         |
| C in Depth with data structure and algorithm |
+----------------------------------------------+
3 rows in set (0.00 sec)

Let us now implement the query to fetch only the first 15 characters −

mysql> select substr(Title,1,15) from DemoTable;

This will produce the following output −

+--------------------+
| substr(Title,1,15) |
+--------------------+
| Introduction to    |
| Introduction to    |
| C in Depth with    |
+--------------------+
3 rows in set (0.00 sec)
raja
Published on 26-Sep-2019 07:00:30
Advertisements