0
0
MySQLquery~10 mins

LENGTH and CHAR_LENGTH in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LENGTH and CHAR_LENGTH
Input String
Output: Byte Count
Input String
Output: Character Count
LENGTH counts bytes in a string, CHAR_LENGTH counts characters. For ASCII, both are same; for multibyte (e.g., UTF-8), LENGTH can be larger.
Execution Sample
MySQL
SELECT LENGTH('hello'), CHAR_LENGTH('hello');
SELECT LENGTH('café'), CHAR_LENGTH('café');
Shows difference between LENGTH and CHAR_LENGTH on ASCII and accented characters.
Execution Table
StepInput StringFunctionResultExplanation
1'hello'LENGTH('hello')5Each ASCII character is 1 byte, total 5 bytes
2'hello'CHAR_LENGTH('hello')55 characters in string
3'café'LENGTH('café')5é is 2 bytes in UTF-8, total bytes = 4 + 1 = 5
4'café'CHAR_LENGTH('café')44 characters total, counting accented as one character
💡 All function calls complete, results show byte vs character counts
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Input String'''hello''hello''café''café''café'
LENGTH Resultnull55555
CHAR_LENGTH Resultnull-5-44
Key Moments - 2 Insights
Why does LENGTH('café') return 5 but CHAR_LENGTH('café') returns 4?
Because LENGTH counts bytes, and 'é' uses 2 bytes in UTF-8, so total bytes are 5. CHAR_LENGTH counts characters, so 'café' has 4 characters.
Are LENGTH and CHAR_LENGTH always the same?
No, they are the same only for single-byte character sets like ASCII. For multibyte characters, LENGTH counts bytes, CHAR_LENGTH counts characters.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the LENGTH('hello') result at step 1?
A6
B5
C4
D1
💡 Hint
Check the 'Result' column in row with Step 1 in execution_table
At which step does CHAR_LENGTH return 4?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Function' and 'Result' columns in execution_table rows
If the input string was only ASCII characters, how would LENGTH and CHAR_LENGTH compare?
ALENGTH would be greater than CHAR_LENGTH
BCHAR_LENGTH would be greater than LENGTH
CBoth would be equal
DCannot determine
💡 Hint
Refer to the concept_flow description about ASCII characters
Concept Snapshot
LENGTH(string) returns number of bytes in string.
CHAR_LENGTH(string) returns number of characters.
For ASCII, both are equal.
For multibyte chars (e.g., UTF-8), LENGTH >= CHAR_LENGTH.
Use CHAR_LENGTH to count characters, LENGTH for bytes.
Full Transcript
This lesson shows how LENGTH and CHAR_LENGTH functions work in MySQL. LENGTH counts the number of bytes in a string, while CHAR_LENGTH counts the number of characters. For example, the string 'hello' has 5 bytes and 5 characters, so both functions return 5. But for 'café', LENGTH returns 5 because the accented 'é' uses 2 bytes in UTF-8 encoding, while CHAR_LENGTH returns 4 because there are 4 characters. This difference is important when working with multibyte character sets. Remember, LENGTH counts bytes, CHAR_LENGTH counts characters.