
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch 5 Characters from Left of String using MySQL SELECT Statement
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)
Advertisements