Char_length Function - How to find number of characters in string in MySQL

Char_length Function - How to find number of characters in string in MySQL

Char_Length is the function in MySQL that can be used to find the number of characters .

Let's create sample table customer and insert some sample data.

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


Let's use Char_Length to get character count for values in different columns.


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


No comments:

Post a Comment