Get the substring of a column in MySQL


Use the SUBSTR() method to get the substring of a column.

Let us first create a table −

mysql> create table DemoTable
-> (
-> Title text
-> );
Query OK, 0 rows affected (0.74 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('This is a MySQL Database');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('Java is an Object Oriented Programming Language');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable ;

Output

This will produce the following output −

+-------------------------------------------------+
| Title                                           |
+-------------------------------------------------+
| This is a MySQL Database                        |
| Java is an Object Oriented Programming Language |
+-------------------------------------------------+
2 rows in set (0.00 sec)

Here is the query to select substring of a column −

mysql> SELECT distinct(substr(Title, 1, 8)) from DemoTable;

Output

This will produce the following output −

+-----------------------+
| (substr(Title, 1, 8)) |
+-----------------------+
| This is               |
| Java is               |
+-----------------------+
2 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

959 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements