Subject Re: [firebird-support] Is this a bug? (FB 2.1 <...> 2.1.2)
Author Ann W. Harrison
Dimitry Sibiryakov wrote:
>> VARCHAR returns a terminated string, CHAR returns a space packed string
>
> VARCHAR has addition short int field to indicate current length,
> length of CHAR is fixed. No one of them has any terminator.
>

True for internal storage, but not true for returned values. Firebird
has an external type CString which is a null terminated string. It's
supported for data transfer and probably for UDFs.

Generally I suggest (when asked, and sometimes other times) that CHAR
be used for fixed length values and values shorter than four bytes.
Thus a US Tax ID which is always 9 digits should be stored as a CHAR,
but a nickname which varies between zero and nine characters should
be stored as a varchar. The postal abbreviations of US state names
should be CHAR(2), but the state names should be varchar(15).

There is one counter argument which is that CHAR compresses better
than VARCHAR with Firebird's run length encoding. Each VARCHAR field
is preceded by a two-byte binary value which contains the number of
actual characters (bytes?) in the string. So, a series of empty
or NULL CHAR fields are contiguous 0x20 values, while empty or null
VARCHAR fields alternate two bytes of 0x00 followed by 0x20 bytes.
(Slightly different with some character sets, but close enough.)
That's an argument for changing the compression algorithm, but not
for using CHAR for long variable length fields.

Cheers,

Ann