Skip to main content

bit_length

1. BIT_LENGTH Function

The BIT_LENGTH function in H2 database is used to determine the number of bits required to represent a given expression.

2. Syntax

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

BIT_LENGTH(expression)

Arguments

  • expression: The expression for which the bit length needs to be calculated. It can be a string, binary value, or BLOB.

Return

  • The BIT_LENGTH function returns the number of bits required to represent the given expression.

3. Notes

  • The BIT_LENGTH function counts the number of bits in the binary representation of the expression.
  • If the expression is a string, the BIT_LENGTH function will count the number of bits required to represent the string's binary representation.
  • If the expression is a binary value or BLOB, the BIT_LENGTH function will count the number of bits directly.
  • The BIT_LENGTH function can be used for various purposes, such as calculating the storage size of binary data or validating the length of binary representations.

4. Examples

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

Example 1 - Calculating the bit length of a string:

SELECT BIT_LENGTH('Hello') AS bit_length;

Output:

bit_length
----------
40

Example 2 - Calculating the bit length of a binary value:

SELECT BIT_LENGTH(X'FF') AS bit_length;

Output:

bit_length
----------
8

Example 3 - Calculating the bit length of a BLOB:

CREATE TABLE files (
id INT PRIMARY KEY,
content BLOB
);

INSERT INTO files VALUES (1, X'FFFF');

SELECT id, BIT_LENGTH(content) AS bit_length
FROM files;

Output:

id | bit_length
---+-----------
1 | 16
  • LENGTH - Calculate the length of a string or binary value
  • char_length - Calculate the number of characters in a string