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

Re: Possible to insert quoted null value into integer field?

From: pablo_tweek(at)yahoo(dot)com (Pablo S)
To: pgsql-general(at)postgresql(dot)org
Subject: Possible to insert quoted null value into integer field?
Date: 2004-08-24 04:13:34
Message-ID: 76d9ca90.0408232013.3e300b84@posting.google.com (view raw)
Hi all, I have search high and low on this - 

Take for instance the statement : 

insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');

On Pg 7.2.x, the db would happily insert the null val into the int
field.  HOWSOMEVER, 7.4.x will explode and error back with:

"DBD::Pg::st execute failed: ERROR: invalid input syntax for integer:
"" at /cgi-bin/foo line xxx"

I see what it is complaining about but I am wondering if there is any
way to roll back this feature, as I have this cheesy bit of perl that
dynamically builds the query and quotes everything and I don't want to
have to change all occurrences to insert the NULL or worse yet try to
differentiate between int/string.

Many thanks,

P

From: Jerry Sievers <jerry(at)jerrysievers(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-24 12:23:14
Message-ID: m38yc4913h.fsf@prod01.jerrysievers.com (view raw)
pablo_tweek(at)yahoo(dot)com (Pablo S) writes:

> Hi all, I have search high and low on this - 
> Take for instance the statement : 
> 
> insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');

Have a look at nullif();

create table foo (a int)
;

insert into foo
values (nullif('$varWhichMayBeEmptyString', '')::int)
;

As I recall, there is a comment in the release notes somewhere between
your PG version and the current one, that int cols no longer take
empty string as NULL.

HTH

-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
                305 321-1144 (mobile	http://www.JerrySievers.com/

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Jerry Sievers <jerry(at)jerrysievers(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-26 02:35:48
Message-ID: 20040826023546.GA31687@svana.org (view raw)
On Tue, Aug 24, 2004 at 08:23:14AM -0400, Jerry Sievers wrote:
> As I recall, there is a comment in the release notes somewhere between
> your PG version and the current one, that int cols no longer take
> empty string as NULL.

IIRC, an empty string was interpreted as a zero, never as a NULL.
It is now (rightfully) rejected...
-- 
Martijn van Oosterhout   <kleptog(at)svana(dot)org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.
From: "gnari" <gnari(at)simnet(dot)is>
To: "Pablo S" <pablo_tweek(at)yahoo(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-26 08:27:59
Message-ID: 007901c48b46$9840c650$0100000a@wp2000 (view raw)
"Pablo S" <pablo_tweek(at)yahoo(dot)com> wrote:


> Hi all, I have search high and low on this - 
> 
> Take for instance the statement : 
> 
> insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');
> 
> On Pg 7.2.x, the db would happily insert the null val into the int
> field.  HOWSOMEVER, 7.4.x will explode and error back with:
> 
> "DBD::Pg::st execute failed: ERROR: invalid input syntax for integer:
> "" at /cgi-bin/foo line xxx"
> 
> I see what it is complaining about but I am wondering if there is any
> way to roll back this feature, as I have this cheesy bit of perl that
> dynamically builds the query and quotes everything and I don't want to
> have to change all occurrences to insert the NULL or worse yet try to
> differentiate between int/string.

if all else fails, you might use a view mirroring the original
table, but with int1 defined as varchar, with rules handling the
conversion at insert/update.

gnari



From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: gnari <gnari(at)simnet(dot)is>
Cc: Pablo S <pablo_tweek(at)Yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-26 20:24:01
Message-ID: 412E46E1.40702@Yahoo.com (view raw)
On 8/26/2004 4:27 AM, gnari wrote:

> "Pablo S" <pablo_tweek(at)yahoo(dot)com> wrote:
> 
> 
>> Hi all, I have search high and low on this - 
>> 
>> Take for instance the statement : 
>> 
>> insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');
>> 
>> On Pg 7.2.x, the db would happily insert the null val into the int
>> field.  HOWSOMEVER, 7.4.x will explode and error back with:

You aren't inserting an SQL NULL value. You try to insert an empty 
string, which is not a valid integer representation.


Jan

>> 
>> "DBD::Pg::st execute failed: ERROR: invalid input syntax for integer:
>> "" at /cgi-bin/foo line xxx"
>> 
>> I see what it is complaining about but I am wondering if there is any
>> way to roll back this feature, as I have this cheesy bit of perl that
>> dynamically builds the query and quotes everything and I don't want to
>> have to change all occurrences to insert the NULL or worse yet try to
>> differentiate between int/string.
> 
> if all else fails, you might use a view mirroring the original
> table, but with int1 defined as varchar, with rules handling the
> conversion at insert/update.
> 
> gnari
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
>                http://archives.postgresql.org


-- 
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck(at)Yahoo(dot)com #

From: Gaetano Mendola <mendola(at)bigfoot(dot)com>
To: Jan Wieck <JanWieck(at)Yahoo(dot)com>
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-26 21:33:46
Message-ID: 412E573A.3010500@bigfoot.com (view raw)
Jan Wieck wrote:

> On 8/26/2004 4:27 AM, gnari wrote:
> 
>> "Pablo S" <pablo_tweek(at)yahoo(dot)com> wrote:
>>
>>
>>> Hi all, I have search high and low on this -
>>> Take for instance the statement :
>>> insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');
>>>
>>> On Pg 7.2.x, the db would happily insert the null val into the int
>>> field.  HOWSOMEVER, 7.4.x will explode and error back with:
> 
> 
> You aren't inserting an SQL NULL value. You try to insert an empty 
> string, which is not a valid integer representation.
> 
> 
> Jan

And IIRC for oracle an empty string is a NULL value  :-(


Regards
Gaetano Mendola





From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: Gaetano Mendola <mendola(at)bigfoot(dot)com>
Cc: PgSQL General ML <pgsql-general(at)postgresql(dot)org>
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-26 22:35:06
Message-ID: 412E659A.4090406@Yahoo.com (view raw)
On 8/26/2004 5:33 PM, Gaetano Mendola wrote:

> Jan Wieck wrote:
> 
>> On 8/26/2004 4:27 AM, gnari wrote:
>> 
>>> "Pablo S" <pablo_tweek(at)yahoo(dot)com> wrote:
>>>
>>>
>>>> Hi all, I have search high and low on this -
>>>> Take for instance the statement :
>>>> insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');
>>>>
>>>> On Pg 7.2.x, the db would happily insert the null val into the int
>>>> field.  HOWSOMEVER, 7.4.x will explode and error back with:
>> 
>> 
>> You aren't inserting an SQL NULL value. You try to insert an empty 
>> string, which is not a valid integer representation.
>> 
>> 
>> Jan
> 
> And IIRC for oracle an empty string is a NULL value  :-(

Who cares about Oracle? They are different things in the ANSI standard.


Jan

-- 
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck(at)Yahoo(dot)com #

From: Gaetano Mendola <mendola(at)bigfoot(dot)com>
To: Jan Wieck <JanWieck(at)Yahoo(dot)com>
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-08-26 22:50:49
Message-ID: 412E6949.1080005@bigfoot.com (view raw)
Jan Wieck wrote:

> On 8/26/2004 5:33 PM, Gaetano Mendola wrote:
> 
>> Jan Wieck wrote:
>>
>>> On 8/26/2004 4:27 AM, gnari wrote:
>>>
>>>> "Pablo S" <pablo_tweek(at)yahoo(dot)com> wrote:
>>>>
>>>>
>>>>> Hi all, I have search high and low on this -
>>>>> Take for instance the statement :
>>>>> insert into foo (text1, text2, int1) values ('Foo', 'Bar', '');
>>>>>
>>>>> On Pg 7.2.x, the db would happily insert the null val into the int
>>>>> field.  HOWSOMEVER, 7.4.x will explode and error back with:
>>>
>>>
>>>
>>> You aren't inserting an SQL NULL value. You try to insert an empty 
>>> string, which is not a valid integer representation.
>>>
>>>
>>> Jan
>>
>>
>> And IIRC for oracle an empty string is a NULL value  :-(
> 
> 
> Who cares about Oracle? They are different things in the ANSI standard.

:-(
^^^


Regards
Gaetano Mendola




From: Jeff Boes <jboes(at)qtm(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Possible to insert quoted null value into integer field?
Date: 2004-09-02 14:14:58
Message-ID: DVFZc.92531$JG7.32916@hydra.nntpserver.com (view raw)
Gaetano Mendola wrote:
> 
>>> And IIRC for oracle an empty string is a NULL value  :-(
>>
>>
>>
>> Who cares about Oracle? They are different things in the ANSI standard.
> 
> 
> :-(
> ^^^
> 

Seems like you could handle this with a rule:

create rule as on insert to my_table
where new.that_column = '' do instead
insert into my_table (col_a, col_b, that_col)
values (new.col_a, new.col_b, NULL);

Or would this break long before the rule got involved, because 
new.that_column has a bad value?

-- 
(Posted from an account used as a SPAM dump. If you really want to get
in touch with me, dump the 'jboes' and substitute 'mur'.)
________
Jeffery Boes <>< jboes(at)qtm(dot)net


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