OCTET_LENGTH

Takes one argument as an input and returns the string length in octets for all string types.

Takes one argument as an input and returns the string length in octets for all string types.

Behavior type

Immutable

Syntax

OCTET_LENGTH ( expression )

Arguments

expression
(CHAR or VARCHAR or BINARY or VARBINARY) is the string to measure.

Notes

  • If the data type of expression is a CHAR, VARCHAR or VARBINARY, the result is the same as the actual length of expression in octets. For CHAR, the length does not include any trailing spaces.

  • If the data type of expression is BINARY, the result is the same as the fixed-length of expression.

  • If the value of expression is NULL, the result is NULL.

Examples

Expression Result
SELECT OCTET_LENGTH(CHAR(10) '1234 '); 4
SELECT OCTET_LENGTH(CHAR(10) '1234'); 4
SELECT OCTET_LENGTH(CHAR(10) ' 1234'); 6
SELECT OCTET_LENGTH(VARCHAR(10) '1234 '); 6
SELECT OCTET_LENGTH(VARCHAR(10) '1234 '); 5
SELECT OCTET_LENGTH(VARCHAR(10) '1234'); 4
SELECT OCTET_LENGTH(VARCHAR(10) ' 1234'); 7
SELECT OCTET_LENGTH('abc'::VARBINARY); 3
SELECT OCTET_LENGTH(VARBINARY 'abc'); 3
SELECT OCTET_LENGTH(VARBINARY 'abc '); 5
SELECT OCTET_LENGTH(BINARY(6) 'abc'); 6
SELECT OCTET_LENGTH(VARBINARY ''); 0
SELECT OCTET_LENGTH(''::BINARY); 1
SELECT OCTET_LENGTH(null::VARBINARY);
SELECT OCTET_LENGTH(null::BINARY);

See also