Re: Significant trailing spaces - Mailing list pgsql-novice

From Tom Lane
Subject Re: Significant trailing spaces
Date
Msg-id 10067.1091457471@sss.pgh.pa.us
Whole thread Raw
In response to Significant trailing spaces  (Steve Tucknott <steve@retsol.co.uk>)
List pgsql-novice
Steve Tucknott <steve@retsol.co.uk> writes:
> I still seem to get the same problem - putting a CHAR(250) variable
> (with trailing spaces) into a VARCHAR(100) gives me a VARCHAR with a
> length 100 - ie padded with spaces. Is this still correct?

This is per SQL specification.  SQL92 section 9.2 "Store assignment"
defines assignment to a varchar thusly:

    [ T is the target variable, V is the value being assigned ]

            d) If the data type of T is variable-length character string
              and the length in characters M of V is not greater than the
              maximum length in characters of T, then the value of T is set
              to V and the length in characters of T is set to M.

            e) If the data type of T is variable-length character string and
              the length in characters M of V is greater than the maximum
              length in characters L of T, then,

              Case:

              i) If the rightmost M-L characters of V are all <space>s, then
                 the value of T is set to the first L characters of V and
                 the length in characters of T is set to L.

             ii) If one or more of the rightmost M-L characters of V are
                 not <space>s, then an exception condition is raised: data
                 exception-string data, right truncation.

If you don't want the spaces, consider using the trim() or rtrim()
function.

            regards, tom lane

pgsql-novice by date:

Previous
From: sql@aniota.com
Date:
Subject: import schema
Next
From: "Scott Marlowe"
Date:
Subject: Re: Help on triggers