Trigger function to change data to correct datatype

From: Arnaud Lesauvage <thewild(at)freesurf(dot)fr>
To: pgsql-novice(at)postgresql(dot)org
Subject: Trigger function to change data to correct datatype
Date: 2006-05-08 13:52:48
Message-ID: 445F4D30.4010209@freesurf.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi list !

I have a MSAccess table in which dates are stored as strings. Some
dates are null but are stored as '-', and I cannot change this
because many queries use this value.
I need to insert theses values in a PostgreSQL table, but with a
real 'date' datatype.
Since '-' is not correct, I thought about creating a trigger that
would change the '-' to NULL before the INSERT took place, but my
function does not work :

CREATE OR REPLACE FUNCTION check_date()
RETURNS "trigger" AS
$BODY$
BEGIN
IF NEW.mydate = '-' THEN
NEW.mydate = NULL;
END IF;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

And :

CREATE TRIGGER check_mydate
BEFORE INSERT
ON mytable
FOR EACH ROW
EXECUTE PROCEDURE check_date();

But when I try to insert a row in this table I have an error :
ERROR: invalid input syntax for type date: "-"

I would like to avoid using a function in the INSERT to replace
the "-" by NULL, because I execute this query on linked tables in
MSAccess, and since both table have the exact same structure, I
use the syntax 'INSERT INTO psql_table SELECT * FROM msaccess_table'.

Is there a way to achieve this ?

Thanks for helping !

Regards
--
Arnaud

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Joao Miguel Ferreira 2006-05-08 13:56:25 Re: database size grows (even after vacuum (full and
Previous Message zvirid traian 2006-05-08 13:35:21 UNSUBSCRIBE