public inbox for [email protected]
help / color / mirror / Atom feedFrom: M Sarwar <[email protected]>
To: David G. Johnston <[email protected]>
Cc: Scott Ribe <[email protected]>
Cc: Wetmore, Matthew (CTR) <[email protected]>
Cc: mahesh <[email protected]>
Cc: Mohammed Aslam <[email protected]>
Cc: [email protected] <[email protected]>
Cc: Tom Lane <[email protected]>
Subject: Re: The same result for with SPACE and without SPACE
Date: Thu, 15 Jun 2023 22:50:12 +0000
Message-ID: <DM4PR19MB5978011B7B0F050B0E6D46DCD35BA@DM4PR19MB5978.namprd19.prod.outlook.com> (raw)
In-Reply-To: <CAKFQuwY8PhVM9f3V=ojqd77mggPXhywLVUo_JU=BE1sg-e33TQ@mail.gmail.com>
References: <DM4PR19MB5978EB964FA1C7B55FFCB02DD35AA@DM4PR19MB5978.namprd19.prod.outlook.com>
<DM4PR19MB59782360E0C452810D70CBE8D35AA@DM4PR19MB5978.namprd19.prod.outlook.com>
<[email protected]>
<DM4PR19MB59784B518F25D2C2292CA3FBD35AA@DM4PR19MB5978.namprd19.prod.outlook.com>
<[email protected]>
<[email protected]>
<[email protected]>
<CAJXhg9ByRV9QPVcW5MPi2c20g948Oxt8qU1DrcVb51fFgTEj2A@mail.gmail.com>
<[email protected]>
<[email protected]>
<[email protected]>
<DM4PR19MB5978B9FAA449068BF3201D20D35BA@DM4PR19MB5978.namprd19.prod.outlook.com>
<CAKFQuwY8PhVM9f3V=ojqd77mggPXhywLVUo_JU=BE1sg-e33TQ@mail.gmail.com>
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 <[email protected]>
Sent: Thursday, June 15, 2023 2:04 PM
To: M Sarwar <[email protected]>
Cc: Scott Ribe <[email protected]>; Wetmore, Matthew (CTR) <[email protected]>; mahesh <[email protected]>; Mohammed Aslam <[email protected]>; [email protected] <[email protected]>; Tom Lane <[email protected]>
Subject: Re: The same result for with SPACE and without SPACE
On Thu, Jun 15, 2023 at 10:44 AM M Sarwar <[email protected]<mailto:[email protected]>> wrote:
Hello Scott and all,
Here the question comes with respect to CHAR(10) to CHARACTeR VARYING( 10 ) comparison results.
char_10 - type character ( 10 )
var_char_10 - type character varying ( 10)
When I do the comparison between char_10 and var_char_10 columns, I may get the wrong results.
var_char_10 is always trimmed from right.
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.
char_10 has padded data of blank or spaces.
Now I compare char10 and var_char_10 columns, I will get the wrong results because char_10 has padded spaces.
Is that correct or will it ignore whitespaces at the end of char_10 column?
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.
view thread (22+ messages)
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: Re: The same result for with SPACE and without SPACE
In-Reply-To: <DM4PR19MB5978011B7B0F050B0E6D46DCD35BA@DM4PR19MB5978.namprd19.prod.outlook.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox