Hi David,
I appreciate your response.
I did the analysis.
It sounds me that in realistic scenario, CHAR_10 and VAR_CHAR_10 columns with the same data will fetch the desired results. It sounds me that there is no conflict.
select length( '123'::char(5)), length( '123 '::char(5)), length('123'::varchar), length('123 '::varchar), length(('123 '::char(5))::varchar), '123 '::char(5) = '123 '::varchar, ('123 '::char(5))::varchar = '123 '::varchar --- this is FALSE which is fine because VARCHAR is not padding the space. ;
Output:- 3 3 3 7 3 true false
Thanks,
Sarwar
From: David G. Johnston <david.g.johnston@xxxxxxxxx>
Sent: Thursday, June 15, 2023 2:04 PM To: M Sarwar <sarwarmd02@xxxxxxxxxxx> Cc: Scott Ribe <scott_ribe@xxxxxxxxxxxxxxxx>; Wetmore, Matthew (CTR) <Matthew.Wetmore@xxxxxxxxxxxxxxxxxxx>; mahesh <mahesha_dba@xxxxxxxxx>; Mohammed Aslam <hmdaslam97@xxxxxxxxx>; pgsql-admin@xxxxxxxxxxxxxxxxxxxx <pgsql-admin@xxxxxxxxxxxxxxxxxxxx>; Tom Lane <tgl@xxxxxxxxxxxxx> Subject: Re: The same result for with SPACE and without SPACE On Thu, Jun 15, 2023 at 10:44 AM M Sarwar <sarwarmd02@xxxxxxxxxxx> wrote:
NO. varchar is never trimmed in this sense. Though casting to a varchar(n) will result in truncation of the input value to n characters - regardless of what those characters are.
Testing shows that the varchar value gets promoted to char, not the other way around.
postgres=# select '123 '::char(5) = '123 '::varchar;
?column? ---------- t (1 row) postgres=# select ('123 '::char(5))::varchar = '123 '::varchar;
?column?---------- f (1 row) No one memorizes char behavior - if you must use it then test your code, and maybe be extra explicit with your casting too.
David J.
|