The following bug has been logged on the website:
Bug reference: 18051
Logged by: Nicolas Gouteux
Email address: nicolas.gouteux@sonarsource.com
PostgreSQL version: 13.7
Operating system: Linux
Description:
Hi
Since SQL inception, char(N) dataype is supposed to add padding blanks up to
N characters, whereas varchar(N) is supposed to truncate extra trailing
white spaces.
This is confirmed by the documentation:
If the string to be stored is shorter than the declared length, values of
type character will be space-padded
https://www.postgresql.org/docs/13/datatype-character.html
However, the following snippet exhibits the exact opposite behavior:
create table if not exists ngx_char (
charcol char(10) not null,
varcharcol varchar(10) not null
);
truncate table ngx_char;
insert into ngx_char (charcol, varcharcol) values ('A', 'A');
insert into ngx_char (charcol, varcharcol) values ('A ', 'A ');
select charcol || 'B', varcharcol || 'C' from ngx_char;
select * from ngx_char where charcol = varcharcol;
Output:
?column?,?column?
AB,AC
AB,A C
This is very strange! Is it me?
Thanks