On 2005-10-21 09:47, Tom Lane wrote:
Alex Turner <[EMAIL PROTECTED]> 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



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

              http://www.postgresql.org/docs/faq

Reply via email to