pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
length of string containing blanks is 0, || behaves differently than concat on string of blanks
3+ messages / 2 participants
[nested] [flat]

* length of string containing blanks is 0, || behaves differently than concat on string of blanks
@ 2019-08-16 08:38 Ehrenreich, Sigrid <Ehrenreich@consist.de>
  2019-08-16 13:22 ` Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Ehrenreich, Sigrid @ 2019-08-16 08:38 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; +Cc: Hanke, Jan-Niklas <Hanke@consist.de>

Hi,

I am not sure, if this is a bug. I think at least it is a documentation bug.
I would expect for concat and || to yield the same results, as long as no NULL is involved. But in my case when I concat a letter and a string of blanks, the results differ.
Is it a bug or is this a feature (then please point me to the documentation, because I could not find it)?

Steps to reproduce:
drop table blubb;
create table blubb (text1 character(3), text2 character(1));
insert into blubb (text1, text2) values ('   ', 'a');
commit;

prod5=> select concat('#',text1,'#'), length(text1), text2 from blubb;
concat | length | text2
--------+--------+-------
#   #  |      0 | a
(1 row)

The string contains three blanks. Its length should be 3 to my understanding.


prod5=> select * from blubb where concat(text1, text2) = '   a';
text1 | text2
-------+-------
       | a
(1 row)

This is what I expect.


prod5=> select * from blubb where text1||text2 = '   a';
text1 | text2
-------+-------
(0 rows)

Now, this comes unexpected. According to documentation || should concatenate both strings, giving the same result as concat above.


Regards,

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@consist.de<mailto:Ehrenreich@consist.de>
Web     www.consist.de<http://www.consist.de/;
[Xing]<http://www.xing.com/companies/consistsoftwaresolutionsgmbh;

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

Consist - IT that works.

Attachments:

image001.jpg (image/jpeg, 1.7K) download view image
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks
  2019-08-16 08:38 length of string containing blanks is 0, || behaves differently than concat on string of blanks Ehrenreich, Sigrid <Ehrenreich@consist.de>
@ 2019-08-16 13:22 ` Tom Lane <tgl@sss.pgh.pa.us>
  2019-08-16 13:34   ` RE: length of string containing blanks is 0, || behaves differently than concat on string of blanks Ehrenreich, Sigrid <Ehrenreich@consist.de>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2019-08-16 13:22 UTC (permalink / raw)
  To: Ehrenreich, Sigrid <Ehrenreich@consist.de>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Hanke, Jan-Niklas <Hanke@consist.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





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* RE: length of string containing blanks is 0, || behaves differently than concat on string of blanks
  2019-08-16 08:38 length of string containing blanks is 0, || behaves differently than concat on string of blanks Ehrenreich, Sigrid <Ehrenreich@consist.de>
  2019-08-16 13:22 ` Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-08-16 13:34   ` Ehrenreich, Sigrid <Ehrenreich@consist.de>
  0 siblings, 0 replies; 3+ messages in thread

From: Ehrenreich, Sigrid @ 2019-08-16 13:34 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Hanke, Jan-Niklas <Hanke@consist.de>

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@consist.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@sss.pgh.pa.us> 
Sent: Friday, August 16, 2019 3:23 PM
To: Ehrenreich, Sigrid <Ehrenreich@consist.de>
Cc: pgsql-bugs@lists.postgresql.org; Hanke, Jan-Niklas <Hanke@consist.de>
Subject: Re: length of string containing blanks is 0, || behaves differently than concat on string of blanks

"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


^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-08-16 13:34 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-16 08:38 length of string containing blanks is 0, || behaves differently than concat on string of blanks Ehrenreich, Sigrid <Ehrenreich@consist.de>
2019-08-16 13:22 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-16 13:34   ` Ehrenreich, Sigrid <Ehrenreich@consist.de>

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