Length Function - How to get Data of String in Bytes in MySQL

Length Function - How to get Data of String in Bytes in MySQL

Length function is used to return the size of string in Bytes in MySQL. It is equivalent to DataLength( ) Function in SQL Server.

Syntax: 


Select Length('StringValue');



Let's say we have customer table with some records and we would like to get the data length for firstname and lastname values.

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 records
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');


use Length( ) function to get the size of column values in Bytes.

Select firstname,length(firstname), lastname,length(lastname),phonenumber,length(phonenumber) from customer;


How to get size of Column in MySQL by using Length ( ) Function - MyQL Tutorial

7 comments: