Return the first n letters of a column in MySQL


To return the first n letters, use the LEFT() function. Following is the syntax −

select left(yourColumnName,yourValue) from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> CourseTitle text
   -> );
Query OK, 0 rows affected (0.65 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(CourseTitle) values('Java with Spring and Hibernate framework');
Query OK, 1 row affected (0.43 sec)

mysql> insert into DemoTable(CourseTitle) values('Python Web Development');
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+----+------------------------------------------+
| Id | CourseTitle                              |
+----+------------------------------------------+
| 1  | Java with Spring and Hibernate framework |
| 2  | Python Web Development                   |
+----+------------------------------------------+
2 rows in set (0.00 sec)

Here is the query to return the first n letters of a column −

mysql> select LEFT(CourseTitle,20) from DemoTable;

Output

+----------------------+
| left(CourseTitle,20) |
+----------------------+
| Java with Spring and |
| Python Web Developme |
+----------------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements