0
0
MySQLquery~5 mins

LENGTH and CHAR_LENGTH in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A4
B6
C5
D0
If 'é' is stored as 2 bytes, what does CHAR_LENGTH('café') return?
A5
B2
C6
D4
Which function counts bytes in a string in MySQL?
ACHAR_LENGTH()
BLENGTH()
CCOUNT()
DSUBSTRING()
What is the difference between LENGTH() and CHAR_LENGTH()?
ALENGTH counts bytes, CHAR_LENGTH counts characters
BLENGTH counts characters, CHAR_LENGTH counts bytes
CBoth count characters
DBoth count bytes
Which function should you use to get the number of characters in a UTF-8 string?
ACHAR_LENGTH()
BLENGTH()
CASCII()
DBIT_LENGTH()
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.