Re: select statement fails - Mailing list pgsql-general

From Tom Lane
Subject Re: select statement fails
Date
Msg-id 2560.1207843045@sss.pgh.pa.us
Whole thread Raw
In response to Re: select statement fails  ("Andrus" <kobruleht2@hot.ee>)
List pgsql-general
"Andrus" <kobruleht2@hot.ee> writes:
> 1. SQL assumes that CHARACTER(n) column is always padded with spaces in
> right.
> So casting to text should preserve spaces.

No, it should not.  In CHAR(n), trailing spaces are semantically
insignificant; 'foo' and 'foo ' are considered equal.  In TEXT
they are just as significant as any other character, and those strings
are definitely not equal.  So 'foo ' as CHAR(4) and 'foo ' as TEXT
do not actually mean the same thing at all, and similarly ' ' means
two different things as CHAR(1) and as TEXT, even though they look
the same.

The SQL spec's definition of CHAR(n) behavior is really pretty broken
in my opinion; you're almost always better off using varchar.  In this
particular case, where you think that a space has semantic significance,
CHAR(n) is simply wrong.

            regards, tom lane

pgsql-general by date:

Previous
From: Erik Jones
Date:
Subject: Re: select statement fails
Next
From: "William Temperley"
Date:
Subject: Re: percentile rank query