LEFT Function - How to get Some Characters from LEFT from String in MySQL

LEFT Function - How to get Some Characters from LEFT from String in MySQL

LEFT Function in MySQL is used to get left part of string with specified length.


LEFT('Value',Length)


Let's create sample table customer with sample data and then we will use Left function on different columns to get only left characters according to the length specified.

CREATE TABLE `customer` (
  `idcustomer` int, 
  `firstname` varchar(50)  NULL,
  `lastname` varchar(30)  NULL,
  `age` int(11) DEFAULT NULL,
  `phonenumber` char(11) DEFAULT NULL,
  `dob` date DEFAULT NULL,
  `gender` char(1) NOT NULL
) ;

-- insert sample data
insert into customer(idcustomer,firstname,lastname,age,phonenumber,dob,gender)
values
(1,'Raza',null,39,'505-4141969','1980-01-01','M'),
(2,'Aamir','Naz',39,'505-4141969','1980-01-01','M'),
(3,'Aamir','Shahzad',39,'505-4141900','1980-01-01','M'),
(4,'Aamir1','Shahzad',39,'505-4141900','1980-01-01','M'),
(5,'Robert','Ladson',69,'505-345900','1960-01-01','M');


Let's get area code those are first 3 characters in phone number and also get Initial Character(First Lettter) from FirstName column.


Select phonenumber,left(phonenumber,3),
firstname,left(firstname,1) from customer;


How to use LEFT Function in MySQL - MySQL Developer Tutorial