On 2005-10-21 09:47, Tom Lane wrote:
> Alex Turner <armtuk@gmail.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
trailingspaces 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