coalesce for null AND empty strings

From: Ferdinand Gassauer <gassauer(at)kde(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: coalesce for null AND empty strings
Date: 2007-03-28 12:50:58
Message-ID: 200703281450.59194.gassauer@kde.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi!

it would be great to have a coalesce2 function which treats empty strings as
null values.

as far as I have seen, there are a lot of comments and coding solutions about
this, but none is an "easy" one and all make the code a bit more complicated
and more difficult to maintain.

I have created this function.
It's similar to nullif, but takes only ONE argument

create or replace function "empty2null"(text_i varchar)
returns varchar as $$
declare
text_p varchar;
begin
if text_i = ''
then text_p := null;
else text_p := text_i;
end if;
return text_p;
end;
$$ LANGUAGE plpgsql;

--
cu
Ferdinand

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jaime Silvela 2007-03-28 13:01:32 unexpected data beyond EOF and character encoding
Previous Message Thomas Burdairon 2007-03-28 12:28:01 Re: Paranthesis and Driver JDBC