pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: mauro <bertolima@yahoo.it>
Cc: pgsql-sql@postgresql.org
Subject: Re: Postgres 8 - problem: invalid input syntax for integer
Date: Thu, 24 Feb 2005 08:14:39 +0000
Message-ID: <421D8CEF.7050900@archonet.com> (raw)
In-Reply-To: <d64dda1e.0502230219.21e243b1@posting.google.com>
References: <d64dda1e.0502230219.21e243b1@posting.google.com>

mauro wrote:
> Hi, In previous version di Postgres (7.2) I used this table:
> CREATE TABLE tablename (id serial, field int1, field2 text);
> 
> Now this query work:
> UPDATE tablename SET field1=''; (NOTE: implicit conversion to 0)
> UPDATE tablename SET field2='';
> 
> (this cause of simple code-generation query - I don't know what's
> field type)

Know your field-types. If you don't know what they are, you can't handle 
errors elegantly anyway.

> Now in postgres 8 this don't work. 
> Why ?(ok, it's the ufficial documentation but I don't understand...
> why? it's so comfortable!)

What number does '' represent?
Does that mean a string of '/2' should equal your number divided by two? 
If not, why not?
Who is providing an empty string where you've asked for a number, and 
why not trap this error (or store a NULL)?

> Can someone help me to create a CAST to re-use this feature?

Well, you could create a function:
CREATE FUNCTION empty_string_is_zero(text) RETURNS integer AS '
   SELECT CASE
     WHEN $1='''' THEN 0
     ELSE $1::integer
   END;
' LANGUAGE SQL;

UPDATE my_table SET myfield=empty_string_is_zero('');

HTH
--
   Richard Huxton
   Archonet Ltd



view thread (4+ messages)  latest in thread

Message-ID: <421D8CEF.7050900@archonet.com>
Permalink:  ../421D8CEF.7050900@archonet.com/
Also on:    postgresql.org/message-id/421D8CEF.7050900@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, bertolima@yahoo.it
  Subject: Re: Postgres 8 - problem: invalid input syntax for integer
  In-Reply-To: <421D8CEF.7050900@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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