RE: length of string containing blanks is 0, || behaves differently than concat on string of blanks

From: "Ehrenreich, Sigrid" <Ehrenreich(at)consist(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>, "Hanke, Jan-Niklas" <Hanke(at)consist(dot)de>
Subject: RE: length of string containing blanks is 0, || behaves differently than concat on string of blanks
Date: 2019-08-16 13:34:39
Message-ID: 11E79AEF1A6944439221BD49C3161C4E6700499E@falkxchg.falkland.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Tom,

Thanks a lot for your explanation.
So, I'll consider this as feature 😉

Regards,
Sigrid

Sigrid Ehrenreich
Dipl.-Informatikerin
Senior Consultant

Consist Software Solutions GmbH
A Consist World Group Company

Christianspries 4, 24159 Kiel, Germany
Telefon  +49 431 / 39 93 - 623
Telefax  +49 431 / 39 93 - 999
E-Mail    Ehrenreich(at)consist(dot)de
Web     www.consist.de

HRB Kiel Nr. 3983
Geschäftsführer: Daniel Ries, Martin Lochte-Holtgreven, Jörg Hansen 

Consist – IT that works.

-----Original Message-----
From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Sent: Friday, August 16, 2019 3:23 PM
To: Ehrenreich, Sigrid <Ehrenreich(at)consist(dot)de>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org; Hanke, Jan-Niklas <Hanke(at)consist(dot)de>
Subject: Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks

"Ehrenreich, Sigrid" <Ehrenreich(at)consist(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2019-08-16 14:20:55 BUG #15961: psql should be able to read password from stdin
Previous Message Tom Lane 2019-08-16 13:22:46 Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks