Recall & Review
beginner
What does the LENGTH() function return in MySQL?
LENGTH() returns the number of bytes used by a string, counting each byte including multi-byte characters.
Click to reveal answer
beginner
What does the CHAR_LENGTH() function return in MySQL?
CHAR_LENGTH() returns the number of characters in a string, counting multi-byte characters as one character each.
Click to reveal answer
intermediate
How do LENGTH() and CHAR_LENGTH() differ when used on a string with multi-byte characters?
LENGTH() counts bytes, so multi-byte characters count as multiple bytes. CHAR_LENGTH() counts characters, so each multi-byte character counts as one.
Click to reveal answer
intermediate
Example: What is the result of LENGTH('café') and CHAR_LENGTH('café') if 'é' is a 2-byte character?
LENGTH('café') returns 5 (4 letters + 1 extra byte for 'é'), CHAR_LENGTH('café') returns 4 (four characters).
Click to reveal answer
intermediate
Why is it important to use CHAR_LENGTH() instead of LENGTH() when counting characters in multi-byte strings?
Because LENGTH() counts bytes, it can overcount characters in multi-byte strings. CHAR_LENGTH() gives the true character count, which is often what you want.
Click to reveal answer
What does LENGTH('hello') return in MySQL?
✗ Incorrect
Each character in 'hello' is one byte, so LENGTH returns 5.
If 'é' is stored as 2 bytes, what does CHAR_LENGTH('café') return?
✗ Incorrect
CHAR_LENGTH counts characters, so 'café' has 4 characters.
Which function counts bytes in a string in MySQL?
✗ Incorrect
LENGTH() counts bytes, CHAR_LENGTH() counts characters.
What is the difference between LENGTH() and CHAR_LENGTH()?
✗ Incorrect
LENGTH counts bytes, CHAR_LENGTH counts characters.
Which function should you use to get the number of characters in a UTF-8 string?
✗ Incorrect
CHAR_LENGTH() returns the number of characters, correctly handling multi-byte UTF-8 characters.
Explain the difference between LENGTH() and CHAR_LENGTH() in MySQL.
Think about how multi-byte characters are stored and counted.
You got /3 concepts.
When should you use CHAR_LENGTH() instead of LENGTH()?
Consider strings with accented or special characters.
You got /3 concepts.