Re: why do i get 2 as answer for select length('aa '::char(6)); - Mailing list pgsql-novice

From David G. Johnston
Subject Re: why do i get 2 as answer for select length('aa '::char(6));
Date
Msg-id CAKFQuwa0SRr0aSySgmU3McM_=L5M=5NKXmdorGKM9Es3TAEvNQ@mail.gmail.com
Whole thread Raw
In response to why do i get 2 as answer for select length('aa '::char(6));  (john snow <ofbizfanster@gmail.com>)
Responses Re: why do i get 2 as answer for select length('aa '::char(6));
List pgsql-novice
On Tuesday, January 16, 2018, john snow <ofbizfanster@gmail.com> wrote:
as well as select length('aa'::char(6));   

i thought if the string to be stored is shorter than specified length , it will be padded with spaces?

i'm using version 10.0 on windows 10

The docs could use more detail here but in short the sentence:

 However, trailing spaces are treated as semantically insignificant and disregarded when comparing two values of type character.

In turn results in the length test only counting semantically significant spaces and thus returning two regardless of the number of input spaces originally present.  postgreSQL pads the spaces but then basically pretends they don't exist except for printing.

I'm not sure why it even bothers to store the spaces given that...but I suppose it's more efficient than looking up the typmod all of the time.

David J.

pgsql-novice by date:

Previous
From: john snow
Date:
Subject: Re: why do i get 2 as answer for select length('aa '::char(6));
Next
From: john snow
Date:
Subject: Re: why do i get 2 as answer for select length('aa '::char(6));