SQL LENGTH Function
Quick summary: The SQL LENGTH() function returns the length of a string in bytes.
SQL LENGTH Syntax
LENGTH(string)
SQL
SQL LENGTH Basic examples
SELECT LENGTH('hello');
SQL
Output:
5
Counts bytes in a string.
SQL LENGTH Real-world usage
SELECT * FROM users WHERE LENGTH(password) < 8;
SQL
Output:
Users with short passwords
Validates data length.
SQL LENGTH Edge cases
SELECT LENGTH('🚀');
SQL
Output:
4
Multibyte characters count as multiple bytes.
SQL LENGTH Common mistakes
Using LENGTH for character count
LENGTH counts bytes, not characters.
Incorrect
LENGTH(name)
Correct
CHAR_LENGTH(name)
Use CHAR_LENGTH for UTF-8 strings.
SQL LENGTH Frequently Asked Questions
What does LENGTH() do in SQL?
Returns the length of a string.
Use case of LENGTH()?
Validating string size.
Counts bytes or characters?
Depends on DB.
Common mistake?
Confusing with CHAR_LENGTH().
Return type?
Integer.
Handles NULL?
Returns NULL.
Performance?
Fast.
Used in WHERE?
Yes.
Alternative?
CHAR_LENGTH.
Used in validation?
Yes.
Supports multibyte?
Depends on function.
Best practice?
Use correct function for encoding.