pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Insert NULL value with to_numeric()
3+ messages / 2 participants
[nested] [flat]

* Insert NULL value with to_numeric()
@ 2006-07-20 00:51  Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb>
  0 siblings, 1 reply; 3+ messages in thread

From: Ghislain Bob Hachey @ 2006-07-20 00:51 UTC (permalink / raw)
  To: pgsql-admin

Hello all,

I'm a newbie.  PostgreSQL 8.1.4. Fedora Core 5.

I'm writing a small java application that will import CSV txt files into
my DB.  The SQL statement I'm sending to pgsql looks like this:

"INSERT into table (col1,col2,col3) values (to_number(?,
'999'),to_timestamp(?, 'MM/DD/YYYY HH24:MI:SS'),to_number(?, '9999');

Values are taken from a String array.

sometimes in col3 (smallint in pgsql DB) I need to insert a NULL value
but when I do I get the following error message:

invalid input syntax for type numeric: " "

I've search the archives and Internet and found similar situations but I
guess not experienced enough to fix this myself with info that I've
found.

Any help would be greatly appreciated.

Regards,

Ghislain Hachey

 




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Insert NULL value with to_numeric()
@ 2006-07-20 01:24  Aaron Bono <postgresql@aranya.com>
  parent: Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb>
  0 siblings, 1 reply; 3+ messages in thread

From: Aaron Bono @ 2006-07-20 01:24 UTC (permalink / raw)
  To: Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb>; +Cc: pgsql-admin

On 7/19/06, Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb> wrote:
>
> Hello all,
>
> I'm a newbie.  PostgreSQL 8.1.4. Fedora Core 5.
>
> I'm writing a small java application that will import CSV txt files into
> my DB.  The SQL statement I'm sending to pgsql looks like this:
>
> "INSERT into table (col1,col2,col3) values (to_number(?,
> '999'),to_timestamp(?, 'MM/DD/YYYY HH24:MI:SS'),to_number(?, '9999');
>
> Values are taken from a String array.
>
> sometimes in col3 (smallint in pgsql DB) I need to insert a NULL value
> but when I do I get the following error message:
>
> invalid input syntax for type numeric: " "
>
> I've search the archives and Internet and found similar situations but I
> guess not experienced enough to fix this myself with info that I've
> found.



Why use to_number or to_timestamp?  I assume you are using setString in your
prepared statement.  In your Java code you can use setNull if the value is
null and setInt or setLong or setTimestamp if it is not.  Then you don't
need the to_number or to_timestamp.

Something like this:

Integer value1;
Date value2;
Integer value3;
PreparedStatement stmt = conn.prepareStatement(
   "INSERT INTO table (col1,col2,col3) " +
   "VALUES (?, ?, ?)"
);
int index = 1;
stmt.setInt(index++, value1.intValue());
stmt.setTimestamp(index++, new java.sql.Timestamp(value2.getTime()));
if (value3 == null) {
   stmt.setNull(index++, Types.INTEGER);
} else {
   stmt.setInt(index++, value3.intValue());
}

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
==================================================================

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Insert NULL value with to_numeric()
@ 2006-07-20 03:48  Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb>
  parent: Aaron Bono <postgresql@aranya.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Ghislain Bob Hachey @ 2006-07-20 03:48 UTC (permalink / raw)
  To: Aaron Bono <postgresql@aranya.com>; +Cc: pgsql-admin


> 
> Why use to_number or to_timestamp?  I assume you are using setString
> in your prepared statement.  In your Java code you can use setNull if
> the value is null and setInt or setLong or setTimestamp if it is not.
> Then you don't need the to_number or to_timestamp. 

You're absolutely right.  I was trying to make it work with setString
first cause I'm new to java and it was easier at first look.  But I will
take your advice.

Thanks a lot for your time

Ghislain Hachey
> 




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2006-07-20 03:48 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-20 00:51 Insert NULL value with to_numeric() Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb>
2006-07-20 01:24 ` Aaron Bono <postgresql@aranya.com>
2006-07-20 03:48   ` Ghislain Bob Hachey <ghachey@pipolfastaem.gov.sb>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox