From: Tom Lane <tgl@sss.pgh.pa.us>
To: Ehrenreich, Sigrid <Ehrenreich@consist.de>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Cc: Hanke, Jan-Niklas <Hanke@consist.de>
Subject: Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks
Date: Fri, 16 Aug 2019 09:22:46 -0400
Message-ID: <8666.1565961766@sss.pgh.pa.us> (raw)
In-Reply-To: <11E79AEF1A6944439221BD49C3161C4E6700430C@falkxchg.falkland.de>
References: <11E79AEF1A6944439221BD49C3161C4E6700430C@falkxchg.falkland.de>
"Ehrenreich, Sigrid" <Ehrenreich@consist.de> writes:
> I am not sure, if this is a bug. I think at least it is a documentation bug.
The char(n) datatype has some odd rules: generally, trailing blanks are
considered insignificant, which is why you're getting 0 for the length.
Trailing blanks will also get stripped whenever the value is converted
to varchar or text, which is what's happening in your || example.
On the other hand, concat() doesn't execute any SQL type conversions;
it just concatenates the I/O representations of the values, so that
the trailing blanks survive.
Yes, all of these things are documented somewhere. The implications
aren't always obvious of course.
Almost always, the correct response to questions like this is "don't
use char(n)". The preferred string type in Postgres is text, or if
you have a reason to have a specific character-length limit (pro tip:
you probably don't), then use varchar(n). In any case, if you think
trailing blanks are valid data, char(n) is not what to store them in.
regards, tom lane
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, Ehrenreich@consist.de, Hanke@consist.de
Subject: Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks
In-Reply-To: <8666.1565961766@sss.pgh.pa.us>
References: <11E79AEF1A6944439221BD49C3161C4E6700430C@falkxchg.falkland.de> <8666.1565961766@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox