Re: why do i get 2 as answer for select length('aa '::char(6));

From: john snow <ofbizfanster(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: why do i get 2 as answer for select length('aa '::char(6));
Date: 2018-01-17 03:03:24
Message-ID: CAE67tvW9_tjMPJE-p3cJw_bw6JtnV8NAAY9JXwYy8x2Lhc++nQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

thanks!

you might have just missed my other post, but could i trouble you for
additional info (if you have any) re:

select length('aa '::varchar(6)); //answers 6

select char_length('aa '::varchar(6)); //answers 6

select char_length('aa '::char(6)); //answers 2 even though the input
string has 6 characters as was the case with the varchar input string

select length('aa '::char(6)); //answers 2 even though the input
string has 6 characters as was the case with the varchar input string

are the results as expected? the last two strike me as unexpected

On Wed, Jan 17, 2018 at 10:52 AM, David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> wrote:

> On Tuesday, January 16, 2018, john snow <ofbizfanster(at)gmail(dot)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.
>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2018-01-17 03:09:16 Re: why do i get 2 as answer for select length('aa '::char(6));
Previous Message David G. Johnston 2018-01-17 02:52:23 Re: why do i get 2 as answer for select length('aa '::char(6));