varchar error

varchar error

am 23.06.2005 10:18:45 von Raul Secan

This is a multi-part message in MIME format.

------=_NextPart_000_000A_01C577E5.51E02F70
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello, I just have this:

CREATE TABLE test (
mytext varchar(5)
) WITHOUT OIDS;

If I put a string with more than 5 chars in mytext, I receive an error, =
regarding the wrong lenght of the string.

In MySQL I know that the string is automatically reduced to the number =
of char allowed by the column, even if I insert a longer string.

I don't want to do this from PHP, and I was wandering how this can be =
done in PostreSQL? Maybe in CREATE TABLE definition?

Cheers, Raul.
------=_NextPart_000_000A_01C577E5.51E02F70
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



charset=3Diso-8859-1">




Hello, I just have=20
this:

 

CREATE TABLE test=20
(
    mytext varchar(5)
) WITHOUT =
OIDS;

 

If I put a string =
with more than 5=20
chars in mytext, I receive an error, regarding the wrong lenght of the=20
string.

 

In MySQL I know that =
the string is=20
automatically reduced to the number of char allowed by the column, even =
if I=20
insert a longer string.

 

I don't want to do =
this from PHP,=20
and I was wandering how this can be done in PostreSQL? Maybe in =
CREATE=20
TABLE definition?

 

Cheers,=20
Raul.


------=_NextPart_000_000A_01C577E5.51E02F70--

Re: varchar error

am 23.06.2005 10:33:47 von Volkan YAZICI

Hi,

On 6/23/05, Raul Secan wrote:
> CREATE TABLE test (
> mytext varchar(5)
> ) WITHOUT OIDS;=20
> =20
> If I put a string with more than 5 chars in mytext, I receive an error,
> regarding the wrong lenght of the string.=20
> =20
> In MySQL I know that the string is automatically reduced to the number of
> char allowed by the column, even if I insert a longer string.

I don't think that it's the function of database to manipulate the input.

> I don't want to do this from PHP, and I was wandering how this can be done
> in PostreSQL? Maybe in CREATE TABLE definition?=20

IMHO, you can create an insert (and update) procedure (like
my_insert() and my_update()) with using substr() [1] function.

[1] www.postgresql.org/docs/8.0/interactive/functions-string.htm l

Regards.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Re: varchar error

am 23.06.2005 10:40:51 von Anthony van den Berg

This is a multi-part message in MIME format.
--------------010607010608060009040805
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

you might try a trigger!


Raul Secan wrote:

> Hello, I just have this:
>
> CREATE TABLE test (
> mytext varchar(5)
> ) WITHOUT OIDS;
>
> If I put a string with more than 5 chars in mytext, I receive an
> error, regarding the wrong lenght of the string.
>
> In MySQL I know that the string is automatically reduced to the number
> of char allowed by the column, even if I insert a longer string.
>
> I don't want to do this from PHP, and I was wandering how this can be
> done in PostreSQL? Maybe in CREATE TABLE definition?
>
> Cheers, Raul.


--

Kaartjeposten.nl
Molukkenstraat 80 / 7512 XT Enschede
* info@kaartjeposten.nl / msn. kaartjeposten@hotmail.com
i. www.kaartjeposten.nl / t. 06 - 28 13 88 57
Nu ook voor uw verjaardagkalenders



--------------010607010608060009040805
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit








you might try a trigger!





Raul Secan wrote:

type="cite">



Hello, I just have
this:

 

CREATE TABLE test (

    mytext varchar(5)

) WITHOUT OIDS;

 

If I put a string
with more than 5 chars in mytext, I receive an error, regarding the
wrong lenght of the string.

 

In MySQL I know
that the string is automatically reduced to the number of char allowed
by the column, even if I insert a longer string.

 

I don't want to do
this from PHP, and I was wandering how this can be done in PostreSQL?
Maybe in CREATE TABLE definition?

 

Cheers, Raul.




--


kaartjeposten

style="font-size: 10pt; font-family: Arial; color: red;">Kaartjeposten.nl

style="font-size: 10pt; font-family: Arial; color: gray;">Molukkenstraat
80 / 7512 XT  Enschede

style="font-size: 10pt; font-family: Wingdings; color: black;"
lang="EN-US">*
style="font-size: 10pt; font-family: Arial; color: gray;" lang="EN-US">
style="font-size: 10pt; font-family: Arial; color: gray;">
/
msn.

i. / t. 06 - 28 13 88 57

style="font-size: 10pt; font-family: Arial; color: red;">Nu ook voor
uw verjaardagkalenders
style="color: navy;">


style="font-size: 10pt; font-family: Arial;">







--------------010607010608060009040805--

Re: varchar error

am 23.06.2005 10:53:09 von Gnanavel Shanmugam

<=21DOCTYPE HTML PUBLIC =22-//W3C//DTD HTML 4.0 =
Transitional//EN=22> content="text/html; charset=3Dutf-8=22> margin-left: 5px; margin-right: 0px;=22>

class="msgHeaders=22>-----Original Message-----
From: =
raul=40zerosoft.ro
Sent: Thu, 23 Jun 2005 11:18:45 =
+0300
To: pgsql-php=40postgresql.org
Subject: =
=5BPHP=5D varchar error

Hello, I just have=20
this:

size="2=22> 

size="2=22>CREATE TABLE test=20
(
    mytext varchar(5)
) WITHOUT OIDS;

size="2=22> 

 

 

 

size="2=22>Cheers,=20
Raul.

=

Re: varchar error

am 23.06.2005 13:29:07 von Marco Colombo

On Thu, 2005-06-23 at 11:18 +0300, Raul Secan wrote:
> Hello, I just have this:
>
> CREATE TABLE test (
> mytext varchar(5)
> ) WITHOUT OIDS;
>
> If I put a string with more than 5 chars in mytext, I receive an
> error, regarding the wrong lenght of the string.
>
> In MySQL I know that the string is automatically reduced to the number
> of char allowed by the column, even if I insert a longer string.
>
> I don't want to do this from PHP, and I was wandering how this can be
> done in PostreSQL? Maybe in CREATE TABLE definition?
>
> Cheers, Raul.

The job of the database is to accept valid data and to refuse invalid
ones, not to silently convert invalid data into a valid form.

While it is possible to do that conversion in PostgreSQL, I suggest you
either reconsider doing it in the application (the place it belongs to),
or think again about the schema (maybe storing the whole string).

BTW, you can also truncate the string at insert time, just change:

INSERT INTO test (mytext) VALUES ('alongstring');

into:

INSERT INTO test (mytext) VALUES (substring('alongstring' for 5));

Here it is, in action:
marco=# CREATE TABLE test (mytext varchar(5)) WITHOUT OIDS;
CREATE TABLE
marco=# INSERT INTO test (mytext) VALUES ('alongstring');
ERROR: value too long for type character varying(5)
marco=# INSERT INTO test (mytext) VALUES (substring('alongstring' for 5));
INSERT 0 1
marco=# SELECT * FROM test;
mytext
--------
along
(1 row)

Of course, you have to do that on every UPDATE, too.

If that's what you want to achieve, I find it much more readable to do
the substring() or the PHP equivalent explicitly, rather than relying on
some implicit RULE or TRIGGER (or worse, on a database that silently
truncates it).

For sure I get puzzled when SELECT returns 'along' after I do INSERT
'alongstring'. Think about consistency.

..TM.
--
____/ ____/ /
/ / / Marco Colombo
___/ ___ / / Technical Manager
/ / / ESI s.r.l.
_____/ _____/ _/ Colombo@ESI.it


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Re: varchar error

am 23.06.2005 17:58:57 von Bruno Wolff III

On Thu, Jun 23, 2005 at 11:18:45 +0300,
Raul Secan wrote:
> Hello, I just have this:
>
> CREATE TABLE test (
> mytext varchar(5)
> ) WITHOUT OIDS;
>
> If I put a string with more than 5 chars in mytext, I receive an error, regarding the wrong lenght of the string.
>
> In MySQL I know that the string is automatically reduced to the number of char allowed by the column, even if I insert a longer string.
>
> I don't want to do this from PHP, and I was wandering how this can be done in PostreSQL? Maybe in CREATE TABLE definition?

The way to do this is to use the substring function to do this. You can extract
the first 5 characters of the string you are supplied.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org