CHAR_LENGTH (also written as CHARACTER_LENGTH) is a standard SQL scalar function that counts the number of characters in its input string and returns that count as an integer. It measures logical characters, so multi-byte UTF-8 or UTF-16 code points are counted as one character each, making it different from OCTET_LENGTH or DATALENGTH functions that measure bytes. CHAR_LENGTH ignores trailing spaces in some systems (for example, MySQL with PAD SPACE mode off) but counts them in others (PostgreSQL). The function works on CHAR, VARCHAR, TEXT, and similar character data types. If the argument is NULL the function returns NULL. When a binary string is supplied, most engines raise a type error.
• string_expression
(STRING) - Any character expression such as a literal, column, or expression; can be CHAR, VARCHAR, TEXT, NCHAR, NVARCHAR, etc.SQL:1999 standard
CHAR_LENGTH counts characters, while OCTET_LENGTH counts bytes, so UTF-8 characters occupying multiple bytes still count as one in CHAR_LENGTH.
It only returns NULL when the input itself is NULL. For an empty string ('') it correctly returns 0.
Behavior depends on the database. PostgreSQL counts them, while MySQL may ignore them unless SQL mode PAD SPACE is enabled.
Use LEN(string) in SQL Server and LENGTH(string) in Oracle, as CHAR_LENGTH is not implemented natively in those systems.