OCTET_LENGTH

OCTET_LENGTH is a SQL function that returns the number of octets (8-bit bytes) in a string. This is particularly useful for measuring the length of binary string values. The value it returns is generally equal to the length of the string in characters for single-byte character set encodings. In multi-byte character set encodings, the octet-length might be greater than character-length.

OCTET_LENGTH(str)

  • str: This is the string value that the OCTET_LENGTH() function will measure. It represents a sequence of characters whose length in octets or bytes you want to find.

Example

SELECT OCTET_LENGTH('MySQL');

Output

5

Explanation

The OCTET_LENGTH function in MySQL is used to return the length of a given string, in bytes. In the above example, the string ‘MySQL’ consists of 5 characters, therefore, the output is 5. Please note that OCTET_LENGTH considers each character as one byte, so it doesn’t account for characters that use more than one byte such as special or Unicode characters.

OCTET_LENGTH(string)

  • string: This parameter is a text or binary string whose octet length is to be returned by the `OCTET_LENGTH` function.

Example

SELECT OCTET_LENGTH('PostgreSQL');

Output

11

Explanation

The OCTET_LENGTH function in PostgreSQL returns the number of bytes in the specified string. In this example, the function returns 11, the number of bytes in the string ‘PostgreSQL’.

OCTET_LENGTH(string)

  • string: It refers to the expression or value whose length has to be calculated. This could be an existing string, a variable, a column of text, etc. The OCTET_LENGTH function computes the number of bytes in the string, taking into account the character set used to encode the string. The string parameter is mandatory; the function cannot be called without it.

Example

SELECT OCTET_LENGTH('Hello World') FROM dual;

Output

11

Explanation

The OCTET_LENGTH function in this context returns the length of the string ‘Hello World’, which is ‘11’. This function is useful for identifying the length of a string in bytes.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.