Skip to main content

octet_length

1. OCTET_LENGTH Function

The OCTET_LENGTH function in H2 database is used to calculate the length of a given string or binary data in bytes.

2. Syntax

The syntax for the OCTET_LENGTH function in H2 database is as follows:

OCTET_LENGTH(str)

Arguments

  • str: The string or binary data for which the length needs to be calculated.

Return

  • The OCTET_LENGTH function returns the length of the given string or binary data in bytes.

3. Notes

  • The OCTET_LENGTH function in H2 database calculates the length of a string or binary data in bytes, rather than characters. This is important when dealing with non-ASCII characters or binary data.
  • The function considers each byte in the input, including any trailing spaces or null characters.
  • If the input parameter is NULL, the function returns NULL.

4. Examples

Here are a few examples demonstrating the usage of the OCTET_LENGTH function in H2 database:

Example 1 - Calculating the length of a string:

SELECT OCTET_LENGTH('Hello') AS length;

Output:

length
------
5

Example 2 - Calculating the length of a binary data column:

CREATE TABLE data (
id INT PRIMARY KEY,
content BINARY
);

INSERT INTO data VALUES (1, X'48656C6C6F'), (2, X'776F726C64');

SELECT id, content, OCTET_LENGTH(content) AS length
FROM data;

Output:

id | content     | length
---+-------------+-------
1 | Hello | 5
2 | world | 5
  • LENGTH - Calculate the length of a string in characters.