Concept Flow - LENGTH and CHAR_LENGTH
Input String
Output: Numeric Value
LENGTH counts bytes in a string, CHAR_LENGTH counts characters. They differ when string has multi-byte characters.
SELECT LENGTH('café'), CHAR_LENGTH('café');
| Step | Function | Input String | Output Value | Explanation |
|---|---|---|---|---|
| 1 | LENGTH | 'café' | 5 | Counts bytes: 'é' is 2 bytes, total 5 bytes |
| 2 | CHAR_LENGTH | 'café' | 4 | Counts characters: 'c','a','f','é' = 4 characters |
| 3 | END | Query finishes after both functions return values |
| Variable | Start | After LENGTH | After CHAR_LENGTH | Final |
|---|---|---|---|---|
| Input String | 'café' | 'café' | 'café' | 'café' |
| LENGTH Result | NULL | 5 | 5 | 5 |
| CHAR_LENGTH Result | NULL | NULL | 4 | 4 |
LENGTH(string) returns the number of bytes in the string.
CHAR_LENGTH(string) returns the number of characters.
They differ when string has multi-byte characters.
Use LENGTH for byte size, CHAR_LENGTH for character count.
Example: LENGTH('café')=5, CHAR_LENGTH('café')=4.LENGTH and CHAR_LENGTH functions in SQL?LENGTH returns the number of bytes used by the string, which can be more than characters if multi-byte characters exist.CHAR_LENGTH returns the number of characters, counting multi-byte characters as one each.LENGTH counts bytes, CHAR_LENGTH counts characters. -> Option Bname using SQL?CHAR_LENGTH is the correct function to count characters in a string column.SELECT CHAR_LENGTH(name) FROM users; is valid SQL syntax to get character count per row.messages with a column text containing the value 'café', what will SELECT LENGTH(text), CHAR_LENGTH(text) FROM messages; return?SELECT CHAR_LENGTH(name) FROM users WHERE LENGTH(name) > 5; but it returns rows even though some names look shorter than 5 characters. What is the likely issue?LENGTH(name) counts bytes, so names with multi-byte characters may have byte length greater than character count.LENGTH(name) > 5 succeeds, so rows match unexpectedly.LENGTH function counts bytes, so multi-byte characters make the length larger than character count. -> Option Acomments where the text length is exactly 10 characters, including multi-byte characters. Which query is correct?CHAR_LENGTH.WHERE CHAR_LENGTH(text) = 10 to filter rows with exactly 10 characters.Pass the test and this pattern moves unknown