
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- How to use MySQL SELECT LEFT to fetch the records containing string with slash
- Fetch some words from the left in MySQL
- How to select all the characters after the first 20 characters from a column in MySQL?
- MySQL query to select a specific string with special characters
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- How to select return value from MySQL prepared statement?
- Change value from 1 to Y in MySQL Select Statement?
- Storing value from a MySQL SELECT statement to a variable?
- MySQL case statement inside a select statement?
- MySQL select query to fetch data with null value?
- Change value from 1 to Y in MySQL Select Statement using CASE?
- How to insert new string within a string subsequent to removing the characters from the original string by using MySQL function?
- Using the value of an alias inside the same MySQL SELECT statement
- How to use the CAST function in a MySQL SELECT statement?
- MySQL query to get a substring from a string except the last three characters?

Advertisements