Thread: Cast '' (blank) to null date

Cast '' (blank) to null date

From
"Mattis Jiderhamn"
Date:
Hi there everyone.
I'm trying to create a trigger so that, if I try to insert blank, that is
'', into a date field it will be converted to null so that I don't get ERROR:  Bad date external representation ''

I believe my ploblem is that I don't know the type of blank (''), and
therefore do not know what to cast from.
It seems it is not unknown.
Any other suggestions?

Thanks, Mattias Jiderhamn mattias@expertsystem.se




Re: Cast '' (blank) to null date

From
Tom Lane
Date:
"Mattis Jiderhamn" <mattias@expertsystem.se> writes:
> I'm trying to create a trigger so that, if I try to insert blank, that is
> '', into a date field it will be converted to null so that I don't get
>   ERROR:  Bad date external representation ''

I don't think it's possible to do that with a trigger.  By the time the
trigger is called, the proposed row has already been formed, so any
input data conversion errors that might occur will already have
occurred.

You'll have to code the substitution on the application side, or perhaps
in the SQL query with a CASE expression.
        regards, tom lane