Re: Converting empty input strings to Nulls - Mailing list pgsql-general

From Martin
Subject Re: Converting empty input strings to Nulls
Date
Msg-id BLU142-DAV5FD7E4A86B1F69EF68E85AEB80@phx.gbl
Whole thread Raw
In response to Converting empty input strings to Nulls  ("Ken Winter" <ken@sunward.org>)
List pgsql-general
Hi Ken-

Have you looked at encode ?
http://www.postgresql.org/docs/8.3/interactive/functions-string.html

Anyone else?
Martin
----- Original Message -----
From: "Ken Winter" <ken@sunward.org>
To: "PostgreSQL pg-general List" <pgsql-general@postgresql.org>
Sent: Saturday, May 31, 2008 1:40 PM
Subject: [GENERAL] Converting empty input strings to Nulls


Applications accessing my PostgreSQL 8.0 database like to submit no-value
date column values as empty strings rather than as Nulls.  This, of course,
causes this PG error:

SQL State: 22007
ERROR: invalid input syntax for type date: ""

I'm looking for a way to trap this bad input at the database level, quietly
convert the input empty strings to Null, and store the Null in the date
column.  I tried a BEFORE INSERT OR UPDATE trigger evoking this function ...

CREATE OR REPLACE FUNCTION "public"."empty_string_to_null"()
RETURNS trigger AS
$BODY$
BEGIN
    IF CAST(NEW.birth_date AS text) = '' THEN
        NEW.birth_date = Null;
    END IF;
    RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

.. but an empty string still evokes the error even before this function is
triggered.

Is there a way to convert empty strings to Nulls before the error is evoked?

~ TIA
~ Ken


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


pgsql-general by date:

Previous
From: Jeff Davis
Date:
Subject: Re: Converting empty input strings to Nulls
Next
From: "Ram Ravichandran"
Date:
Subject: Defining character sets for indicidual fields