Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hycC1-0002ix-24 for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Aug 2019 13:22:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hycBy-0001CU-UK for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Aug 2019 13:22:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hycBy-00015A-Ia for pgsql-bugs@lists.postgresql.org; Fri, 16 Aug 2019 13:22:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hycBw-0008L7-67 for pgsql-bugs@lists.postgresql.org; Fri, 16 Aug 2019 13:22:53 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x7GDMkrL008667; Fri, 16 Aug 2019 09:22:47 -0400 From: Tom Lane To: "Ehrenreich, Sigrid" cc: "pgsql-bugs@lists.postgresql.org" , "Hanke, Jan-Niklas" Subject: Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks In-reply-to: <11E79AEF1A6944439221BD49C3161C4E6700430C@falkxchg.falkland.de> References: <11E79AEF1A6944439221BD49C3161C4E6700430C@falkxchg.falkland.de> Comments: In-reply-to "Ehrenreich, Sigrid" message dated "Fri, 16 Aug 2019 08:38:02 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8665.1565961766.1@sss.pgh.pa.us> Date: Fri, 16 Aug 2019 09:22:46 -0400 Message-ID: <8666.1565961766@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Ehrenreich, Sigrid" 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