BIT_LENGTH

Return the number of bits in the input string's UTF-8 encoding.

Category: stringReturns: INTEGERDialect: Standard

Syntax

BIT_LENGTH(str)

Description

## Overview Returns the number of bits in the UTF-8 encoding of the input string. BIT_LENGTH always equals OCTET_LENGTH multiplied by 8. It is most useful for cryptographic and protocol work where bit-level sizes matter, for example reasoning about block boundaries, MAC input sizes, or wire format padding. For character counts use CHAR_LENGTH, for byte counts use OCTET_LENGTH; BIT_LENGTH exists primarily for SQL-standard completeness. ## Behavior - Returns NULL when the input is NULL. - Returns 0 for an empty string. - Always equals OCTET_LENGTH(str) * 8. - ASCII characters contribute 8 bits each; UTF-8 multi-byte sequences contribute 16, 24, or 32 bits. - Applies to the canonical UTF-8 form of the string. Different normalisation forms may yield different bit counts for visually identical inputs. - Accepts VARBINARY and BYTEA types, in which case it returns bytes times 8. ## Compatibility - Conforms to the SQL standard BIT_LENGTH definition. - Assumes UTF-8 storage encoding.

Parameters

NameTypeDescription
strSpecifies the input string whose UTF-8 bit length is computed.

Examples

-- ASCII: 8 bits per character
SELECT BIT_LENGTH('hello');  -- 40
-- Empty string
SELECT BIT_LENGTH('');  -- 0
-- Single 2-byte UTF-8 character contributes 16 bits
SELECT BIT_LENGTH('é');  -- 16
-- Single 4-byte emoji contributes 32 bits
SELECT BIT_LENGTH('😀');  -- 32
-- NULL propagates
SELECT BIT_LENGTH(CAST(NULL AS VARCHAR));  -- NULL
-- Relationship to OCTET_LENGTH
SELECT BIT_LENGTH('café') = OCTET_LENGTH('café') * 8;  -- true

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →