OCTET_LENGTH is a scalar string function defined in the SQL standard that calculates the size of a character or binary value in bytes, not characters. It is useful when multi-byte encodings (such as UTF-8 or UTF-16) mean a string’s byte length differs from its character count. If the argument is NULL, the function returns NULL. When applied to fixed-length types (e.g., CHAR(10)), trailing pad bytes are generally included, while for varying types (e.g., VARCHAR) only the stored bytes are counted. Behavior is deterministic and has no side effects.
string_or_binary_expression
- string, binary, or expression that resolves to those types. Required.CHAR_LENGTH, LENGTH, DATALENGTH (SQL Server), BIT_LENGTH, CHARACTER SET
SQL-92 standard
Databases that store text in UTF-8 or UTF-16 will report larger byte counts for characters outside the ASCII range because those characters take multiple bytes.
Yes. For fixed-length CHAR columns, most databases pad with spaces, and OCTET_LENGTH counts those pad bytes.
If your database allows passing binary columns to OCTET_LENGTH, it will return the byte size of the BLOB. Otherwise, use a binary-specific size function.
Add a CHECK constraint: `CHECK (OCTET_LENGTH(col) <= 1048576)` to reject rows where `col` exceeds 1 MB in storage.