Thread: VARCHAR truncation

VARCHAR truncation

From
"David Wall"
Date:
Is there a way in a table with, for example, a VARCHAR(250) to have it
truncate any string I send that's longer?  Right now, it throws an
exception: ERROR:  value too long for type character varying(250)

I'd prefer that my mainline code doesn't know about the lengths of strings
in the database because they sometimes vary depending on the db backend
(Oracle only allows one LONG string, for example, so if my PG has two, these
are often ported as under Oracle as two VARCHAR2 fields).

Thanks,
David



Re: VARCHAR truncation

From
"David Wall"
Date:
From the docs, I read:

+++

SQL defines two primary character types: character(n) and character
varying(n), where n is a positive integer. Both of these types can store
strings up to n characters in length. An attempt to store a longer string
into a column of these types will result in an error, unless the excess
characters are all spaces, in which case the string will be truncated to the
maximum length. (This somewhat bizarre exception is required by the SQL
standard.) If the string to be stored is shorter than the declared length,
values of type character will be space-padded; values of type character
varying will simply store the shorter string.

Note: Prior to PostgreSQL 7.2, strings that were too long were silently
truncated, no error was raised.

 +++

This is the "problem" that I've run into since we recently moved from 7.1 to
7.2.  Is there any way to get the non-standard behavior back?

David



Re: VARCHAR truncation

From
Andrew Sullivan
Date:
On Mon, Nov 18, 2002 at 07:45:39PM -0800, David Wall wrote:
> This is the "problem" that I've run into since we recently moved from 7.1 to
> 7.2.  Is there any way to get the non-standard behavior back?

Why not change to type text()?

A

--
----
Andrew Sullivan                         204-4141 Yonge Street
Liberty RMS                           Toronto, Ontario Canada
<andrew@libertyrms.info>                              M2P 2A8
                                         +1 416 646 3304 x110