MySQL select statement to fetch 5 characters from the left of the string


To fetch number of characters from the left of the string, use the LEFT method in MySQL. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Sam Brown');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('Adam Smith');
Query OK, 1 row affected (7.01 sec)
mysql> insert into DemoTable values('Carol Taylor');
Query OK, 1 row affected (0.68 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
| Name         |
+--------------+
| Sam Brown    |
| David Miller |
| Adam Smith   |
| Carol Taylor |
+--------------+
4 rows in set (0.00 sec)

Following is the SELECT to fetch 5 characters from the left of a string −

mysql> select left(Name,5) from DemoTable order by Name;

This will produce the following output −

+--------------+
| left(Name,5) |
+--------------+
| Adam         |
| Carol        |
| David        |
| Sam B        |
+--------------+
4 rows in set (0.46 sec)

Updated on: 24-Sep-2019

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements