From: | "M(dot) van Egmond" <mvanegmond(at)quetalzorg(dot)nl> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Type cast text to int4 |
Date: | 2007-09-07 09:23:01 |
Message-ID: | 46E11875.402@quetalzorg.nl |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hello everybody,
Im having troubles overriding postgresql's default behaviour of throwing
an error while trying to cast an empty string to int4. I would like to
cast empty strings to a null integer value. I've tried this by creating
my own domain:
CREATE DOMAIN my_int AS integer;
-- Then created a pl/pgsql function that handles my type
CREATE OR REPLACE FUNCTION text2my_int(text)
RETURNS my_int AS
$BODY$BEGIN
if $1='' then
return NULL;
end if;
return $1::int4;
END;$BODY$
LANGUAGE 'plpgsql' IMMUTABLE STRICT;
-- Then added the typecast for my type.
CREATE CAST (text AS my_int) WITH FUNCTION text2my_int(text) AS IMPLICIT;
-- Testing the new cast:
select ''::my_int;
-- The expected result should be a NULL value, instead i get an ERROR:
--
-- ERROR: invalid input syntax for integer: ""
-- SQL status:22P02
Does anyone have a clue how i can cast empty string to a NULL integer
value by just using a pl/pgsql function?
Thanks!
Matthieu van Egmond
From | Date | Subject | |
---|---|---|---|
Next Message | M. van Egmond | 2007-09-07 09:36:23 | Type cast text to int4 |
Previous Message | Albe Laurenz | 2007-09-07 09:16:02 | Re: dblink vs dbi-link (and errors compiling) |