Re: The same result for with SPACE and without SPACE

From: Scott Ribe <scott_ribe(at)elevated-dev(dot)com>
To: M Sarwar <sarwarmd02(at)outlook(dot)com>
Cc: "pgsql-admin(at)lists(dot)postgresql(dot)org" <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: The same result for with SPACE and without SPACE
Date: 2023-06-14 22:47:21
Message-ID: 9358F9A5-2926-4046-AD48-0C83C790A523@elevated-dev.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-advocacy

> On Jun 14, 2023, at 4:31 PM, M Sarwar <sarwarmd02(at)outlook(dot)com> wrote:
>
> I expect this to behave the same way whether it comes from CHAR or VARCHAR.

Why would you expect that??? CHAR explicitly means that:

1) shorter strings will be space-padded to the given length
2) if a string is presented with spaces making it longer, it will be truncated

Putting it all together, it implies:

3) trailing spaces are semantically meaningless

Anyway, the closest I could to finding a reference on comparison behavior for this case states: "The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them."

And there you go, you used CHAR(10), so all values in the table are space-padded to length 10, so for comparison *any* value will be space-padded to 10.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Scott Ribe 2023-06-14 22:54:50 Re: The same result for with SPACE and without SPACE
Previous Message Tom Lane 2023-06-14 22:45:39 Re: The same result for with SPACE and without SPACE

Browse pgsql-advocacy by date

  From Date Subject
Next Message Scott Ribe 2023-06-14 22:54:50 Re: The same result for with SPACE and without SPACE
Previous Message Tom Lane 2023-06-14 22:45:39 Re: The same result for with SPACE and without SPACE