![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements