DBAtlas · Database operations atlas

22026: string_data_length_mismatch

PostgreSQL

What does it mean?

The length of a bit string does not match the fixed length required by the operation. In PostgreSQL it most often appears when a bit string of a different length is written to a fixed-length bit(n) type, or when bitwise AND/OR/XOR is applied to bit strings of different lengths.

Why does it happen?

Writing a bit string shorter/longer than n to a bit(n) column (e.g. B'101' into a bit(4) column: 'bit string length 3 does not match type bit(4)'), or applying &, |, # to bit strings of different lengths ('cannot AND bit strings of different sizes'). It is not caused by text or character encoding; an overlong value for char(n)/varchar(n) raises 22001 instead.

How to fix it

Bring the value to the target length: use an explicit cast (B'101'::bit(4) pads with zeros on the right and truncates any excess), or define the column as bit varying(n) if the length can vary. Cast the operands to the same length before bitwise operations; check the length with length().

Open in DBAtlas →

More from this engine