OCTET_LENGTH function - Amazon Redshift

OCTET_LENGTH function

Returns the length of the specified string as the number of bytes.

Syntax

OCTET_LENGTH(expression)

Argument

expression

The input parameter is a CHAR, VARCHAR, or VARBYTE string.

Return type

The OCTET_LENGTH function returns an integer indicating the number of bytes in the input string.

If the input string is a character string, the LEN function returns the actual number of characters in multi-byte strings, not the number of bytes. For example, to store three four-byte Chinese characters, you need a VARCHAR(12) column. The LEN function will return 3 for that same string.

Usage notes

Length calculations do not count trailing spaces for fixed-length character strings but do count them for variable-length strings.

Example

The following example returns the number of bytes and the number of characters in the string français.

select octet_length('français'), len('français'); octet_length | len --------------+----- 9 | 8 (1 row)