473,503 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Possible to insert quoted null value into integer field?

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
Nov 23 '05 #1
8 14639
pa*********@yahoo.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/
Nov 23 '05 #2
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 <kl*****@svana.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.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFBLUyCY5Twig3Ge+YRApw1AJ4+lqRpXcdOPXA6q23pSV Ew5gKrHgCgzAGI
mq64EH4UCZ3YkKZaklgKqkg=
=TzYW
-----END PGP SIGNATURE-----

Nov 23 '05 #3
"Pablo S" <pa*********@yahoo.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

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

http://archives.postgresql.org

Nov 23 '05 #4
On 8/26/2004 4:27 AM, gnari wrote:
"Pablo S" <pa*********@yahoo.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. #
#================================================= = Ja******@Yahoo.com #

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5
Jan Wieck wrote:
On 8/26/2004 4:27 AM, gnari wrote:
"Pablo S" <pa*********@yahoo.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


Nov 23 '05 #6
On 8/26/2004 5:33 PM, Gaetano Mendola wrote:
Jan Wieck wrote:
On 8/26/2004 4:27 AM, gnari wrote:
"Pablo S" <pa*********@yahoo.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. #
#================================================= = Ja******@Yahoo.com #

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #7
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" <pa*********@yahoo.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

Nov 23 '05 #8
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 <>< jb***@qtm.net
Nov 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
9994
by: M Wells | last post by:
Hi All, I have a table that holds pregenerated member IDs. This table is used to assign an available member id to web site visitors who choose to register with the site So, conceptually the...
22
3006
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
9
3449
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
7
2420
by: Arek | last post by:
Hey, I am inserting values in the table: Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks (idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text & "')", conn)...
4
2262
by: chambersdon | last post by:
I have an application that needs to insert nulls into the database and I don't seem to be able to do this. I am currently trying to do this with a Typed DataSet but I can't seem to Insert Nulls...
3
2182
by: 4partee | last post by:
I'm trying to import a csv file with a PHP procedure. However, some of the lines in the csv file have missing values. When this command is given to mysql: insert tablex values...
7
7888
by: usenet | last post by:
I would like, if it's possible, to set the value of a field in a table to a number of spaces. One space would be fine, I just want to be able to set the field to a default value that's not NULL...
3
7654
by: Davor | last post by:
I'm trying to import data from flat file in table and have few problems. 1. Field Delimiter is ',' (comma). If ',' occurs in quoted string it is still treated as field delimiter. This is BUG or...
6
3439
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.