Blank-padding (was: Oracle buys Innobase)

From: "Dean Gibson (DB Administrator)" <postgresql4(at)ultimeth(dot)com>
To: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Blank-padding (was: Oracle buys Innobase)
Date: 2005-10-21 23:28:48
Message-ID: 435979B0.60107@ultimeth.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On 2005-10-21 09:47, Tom Lane wrote:
> Alex Turner <armtuk(at)gmail(dot)com> writes:
>
>> It appears that casting to a char() causes spaces to be stripped (ignored) from the string:
>>
> mls=# select length('123 '::char(8));
> length
> --------
> 3
> (1 row)
>
>
>> I'm not sure about anyone else, but I would personaly consider that a bug?
>>
>
> No, it's a feature, as per extensive discussion some time ago when we made it do that. The general rule is that trailing spaces in a char(n) are semantically insignificant.
>
> regards, tom lane
>

I remember that discussion, and I was for the change. However, upon
doing some testing after reading the above, I wonder if the
blank-stripping isn't too aggressive. I have a CHAR(6) field (say,
named Z) that has "abc " in it. Suppose I want to append "x" to Z,
with any leading spaces in Z PRESERVED. The following do not work in 8.0.4:

select Z || 'x';

select Z::char(6) || 'x';

select Z::varchar(6) || 'x';

select (Z || ' ')::char(6) || 'x';

There are only two ways I've found:

select rpad( Z, 6) || 'x'; -- but "rpad" is apparently not a
SQL-standard function.

select cast (Z || ' ' as varchar(6)) || 'x'; -- hokey but
SQL-compliant

Is there something I'm missing???

-- Dean

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dean Gibson (DB Administrator) 2005-10-21 23:34:55 Re: VACUUM anomoly: FIXED in 8.0.4
Previous Message Scott Marlowe 2005-10-21 23:14:53 Re: How much slower are numerics?

Browse pgsql-sql by date

  From Date Subject
Next Message Volkan YAZICI 2005-10-21 23:30:02 Re: Reading bytea field
Previous Message Tom Lane 2005-10-21 22:14:00 Re: Delete rule chain stops unexpectedly