0
0
SQLquery~5 mins

LENGTH and CHAR_LENGTH in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the LENGTH function do in SQL?
LENGTH returns the number of bytes used by a string. It counts the storage size, which may differ from the number of characters if the string uses multi-byte characters.
Click to reveal answer
intermediate
What is the difference between LENGTH and CHAR_LENGTH in SQL?
LENGTH counts bytes, while CHAR_LENGTH counts the number of characters in a string. For single-byte characters, they are the same, but for multi-byte characters, CHAR_LENGTH shows the actual character count.
Click to reveal answer
intermediate
How does CHAR_LENGTH handle multi-byte characters?
CHAR_LENGTH counts each character once, regardless of how many bytes it uses. So, a character like 'é' counts as 1 character even if it uses multiple bytes.
Click to reveal answer
beginner
Write a SQL query to find the number of characters in the column 'name' from the table 'users'.
SELECT CHAR_LENGTH(name) AS character_count FROM users;
Click to reveal answer
beginner
If a string contains only ASCII characters, what will LENGTH and CHAR_LENGTH return?
Both LENGTH and CHAR_LENGTH will return the same number because ASCII characters use one byte each.
Click to reveal answer
What does LENGTH('hello') return in SQL?
ADepends on the character encoding
B6
C4
D5
Which function counts the number of characters, not bytes, in a string?
ALENGTH
BCHAR_LENGTH
CSUBSTRING
DASCII
If a string contains the character 'é' (which uses 2 bytes), what will LENGTH('é') return?
A2
B1
C0
DDepends on the database
What will CHAR_LENGTH('é') return?
A1
B2
C0
DDepends on the database
Which SQL function would you use to get the byte size of a string?
ACHAR_LENGTH
BCOUNT
CLENGTH
DSUBSTRING
Explain the difference between LENGTH and CHAR_LENGTH in SQL with an example.
Think about how characters like 'é' are stored.
You got /4 concepts.
    How would you find the number of characters in a text column that may contain multi-byte characters?
    Focus on counting characters, not bytes.
    You got /3 concepts.