char_length function in MySQL query

CHAR_LENGTH() counts characters. Compare it with LENGTH(), which counts bytes.

Use CHAR_LENGTH() to count the characters in a string. Syntax
CHAR_LENGTH('string');
Example
SELECT CHAR_LENGTH('plus2net.com');
The output is 12
We will apply this to our student table
Let us find out the length of name present in our student table.
SELECT id, name, CHAR_LENGTH(name) AS LN FROM `student`
We will get a list of id , name and length like this
id name LN
1John Deo8
2 Max Ruin 8
3 Arnold 6
We have seen how to use char_length in SELECT queries. Now let us use them inside WHERE Query

Using char_length with WHERE

The following example finds rows where the value becomes at most one character after conversion to text.
SELECT id, name FROM `student` WHERE CHAR_LENGTH(mark) <=1
We can combine two columns by using CONCAT() function and then find out the length of the total string.
SELECT id,CONCAT(name,'-',class) AS Name , 
 CHAR_LENGTH(CONCAT(name,'-',class) ) AS LN FROM `student`
Few rows are shown here.
idNameLN
1John Deo-Four13
2Max Ruin-Three14
-----
34Gain Toe-Seven14
35Rows Noump-Six14
39-1





Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer