pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dean Gibson (DB Administrator) <postgresql4@ultimeth.com>
To: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Blank-padding (was: Oracle buys Innobase)
Date: Fri, 21 Oct 2005 16:28:48 -0700
Message-ID: <435979B0.60107@ultimeth.com> (raw)
In-Reply-To: <23805.1129913241@sss.pgh.pa.us>
References: <CC1CF380F4D70844B01D45982E671B239E8BEC@mtxexch01.add0.masergy.com>
	<878xwokk4d.fsf@charter.net>
	<20051020214518.Q1006@ganymede.hub.org>
	<33c6269f0510210716x474dae2ah3861d32d8db01602@mail.gmail.com>
	<23805.1129913241@sss.pgh.pa.us>

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 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





view thread (2+ messages)  latest in thread

Message-ID: <435979B0.60107@ultimeth.com>
Permalink:  ../435979B0.60107@ultimeth.com/
Also on:    postgresql.org/message-id/435979B0.60107@ultimeth.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql4@ultimeth.com
  Subject: Re: Blank-padding (was: Oracle buys Innobase)
  In-Reply-To: <435979B0.60107@ultimeth.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox