Thread: BUG #3561: CHAR(x) blank stripped. Server or documentation bug?

BUG #3561: CHAR(x) blank stripped. Server or documentation bug?

From
"Alexander Kirpa"
Date:
The following bug has been logged online:

Bug reference:      3561
Logged by:          Alexander Kirpa
Email address:      postgres@bilteks.com
PostgreSQL version: 8.2.4
Operating system:   FreeBSD 6.2
Description:        CHAR(x) blank stripped. Server or documentation bug?
Details:

Regarding
http://www.postgresql.org/docs/8.2/interactive/datatype-character.html
char(n) - fixed-length, blank padded
In live:
CREATE TABLE test1 (a character(6),b varchar(6));
INSERT INTO test1 VALUES ('ok','ok');
INSERT INTO test1 VALUES (' bad ',' bad ');
INSERT INTO test1 VALUES ('  poor','  poor');
select '['||a||']' as aa,'['||b||']' as bb from test1;
INSERT INTO test1 VALUES ('ok','ok');
INSERT INTO test1 VALUES (' bad ',' bad ');
INSERT INTO test1 VALUES ('  poor','  poor');
select '['||a||']' as aa,'['||b||']' as bb from test1;
    aa    |    bb
----------+----------
 [ok]     | [ok]
 [ bad]   | [ bad ]
 [  poor] | [  poor]
(3 rows)
It is easy to see that char(x) field really blank stripped.
This is bug of server or documentation?

Best regards,
 Alexander Kirpa

Re: BUG #3561: CHAR(x) blank stripped. Server or documentation bug?

From
Gregory Stark
Date:
"Alexander Kirpa" <postgres@bilteks.com> writes:

> select '['||a||']' as aa,'['||b||']' as bb from test1;
>     aa    |    bb
> ----------+----------
>  [ok]     | [ok]
>  [ bad]   | [ bad ]
>  [  poor] | [  poor]
> (3 rows)
> It is easy to see that char(x) field really blank stripped.
> This is bug of server or documentation?

From the documentation:

> Values of type character are physically padded with spaces to the specified
> width n, and are stored and displayed that way. However, the padding spaces
> are treated as semantically insignificant. Trailing spaces are disregarded
> when comparing two values of type character, and they will be removed when
> converting a character value to one of the other string types. Note that
> trailing spaces are semantically significant in character varying and text
> values.

Concatenation is an example of an operation where the padding spaces are
treated as semantically insignificant, so they get removed before the
concatenation.

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com