Skip site navigation (1) Skip section navigation (2)

Re: CHAR(n) always trims trailing spaces in 7.4

From: "Jeremy Smith" <jer(at)highboard(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: CHAR(n) always trims trailing spaces in 7.4
Date: 2004-02-18 18:06:32
Message-ID: FKEOIJEHOIKIOEHCGNCLKEMBEDAA.jer@highboard.com (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-sql
Also, to make char(n) even more annoying, I had the one character value "K"
stored in a column that was char(2).  When I pulled it from the database and
tried to compare it to a variable with a value of "K" it came out inequal.
Of course in mysql, that was not a problem.

Jeremy

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of Richard Huxton
Sent: Wednesday, February 18, 2004 4:40 AM
To: Tom Lane; scott.marlowe
Cc: elein; news.postgresql.org; pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] CHAR(n) always trims trailing spaces in 7.4


On Wednesday 18 February 2004 00:25, Tom Lane wrote:
> "scott.marlowe" <scott(dot)marlowe(at)ihs(dot)com> writes:
> > But then this:
> > select 'x'||' '||'x'
> > should produce xx, but it produces x x.
>
> No, because the imputed type of those literals is text.  You'd have to
> cast the middle guy to char(n) explicitly to make its trailing spaces go
> away when it's reconverted to text.
>
> The real issue here is that trailing spaces in char(n) are semantically
> insignificant according to the SQL spec.  The spec is pretty vague about
> which operations should actually honor that insignificance --- it's
> clear that comparisons should, less clear about other things.  I think
> the 7.4 behavior is more consistent than what we had before, but I'm
> willing to be persuaded to change it again if someone can give an
> alternate definition that's more workable than this one.

[rant on]

I've never really understood the rationale behind char(n) in SQL databases
(other than as backward compatibility with some old mainframe DB).
Insignificant spaces? If it's not significant, why is it there? You could
have a formatting rule that specifies left-aligned strings space-padded (as
printf) but that's not the same as mucking about appending and trimming
spaces.

The only sensible definition of char(n) that I can see would be:
A text value of type char(n) is always "n" characters in length. If you
assign
less than "n" characters, it is right-padded with spaces. In all other
respects it behaves as any other text type of length "n" with right-trailing
spaces.

[rant off - ah, feel better for that :-]

--
  Richard Huxton
  Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



In response to

Responses

pgsql-hackers by date

Next:From: Bruce MomjianDate: 2004-02-18 18:23:25
Subject: Re: NO WAIT ...
Previous:From: Tom LaneDate: 2004-02-18 18:06:27
Subject: Re: NO WAIT ...

pgsql-sql by date

Next:From: Jeremy SmithDate: 2004-02-18 19:25:19
Subject: Inserting NULL into Integer column
Previous:From: Tom LaneDate: 2004-02-18 17:27:29
Subject: Re: [SQL] 7.4 - FK constraint performance

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group