PostgreSQL octet_length() Function

The PostgreSQL octet_length() function returns the length of a given string in bytes.

If you want to get the number of bits in a string, use the bit_length() function.

If you want to get the number of characters in the string, use char_length(), or character_length() or length().

octet_length() Syntax

This is the syntax of the PostgreSQL octet_length() function:

octet_length(string)

Parameters

string

Required. The string to process.

Return value

The PostgreSQL octet_length() function returns an integer that is the length of a given string in bytes.

octet_length() Examples

Simple example

SELECT
    'Bytes' AS "String",
    octet_length('a') AS "a",
    octet_length('string') AS "string",
    octet_length('01') AS "01",
    octet_length('你') AS "你";
 String | a | string | 01 | 你
--------+---+--------+----+----
 Bytes  | 1 |      6 |  2 |  3

octet_length() vs octet_length()

For the same string, the return value of bit_length() is 8 times the return value of octet_length(). For example:

SELECT
    'ab' AS " ",
    bit_length('ab') AS "Bits",
    octet_length('ab') AS "Bytes";
    | Bits | Bytes
----+------+-------
 ab |   16 |     2