Re: 8.0.3 regexp_replace()... - Mailing list pgsql-general

From rlee0001
Subject Re: 8.0.3 regexp_replace()...
Date
Msg-id 1138649243.775197.248880@g14g2000cwa.googlegroups.com
Whole thread Raw
In response to 8.0.3 regexp_replace()...  ("rlee0001" <robeddielee@hotmail.com>)
Responses Re: 8.0.3 regexp_replace()...
Re: 8.0.3 regexp_replace()...
List pgsql-general
I did get the code working. The function DDL follows:

CREATE OR REPLACE FUNCTION "webadmin"."regexp_replacex" (source
varchar, pattern varchar, replacement varchar) RETURNS varchar AS
$body$
DECLARE
 retvalue VARCHAR;
BEGIN
 retvalue = "source";
 LOOP
  retvalue = REPLACE(retvalue, COALESCE(SUBSTRING(retvalue FROM
"pattern"), ''), "replacement");
  EXIT WHEN retvalue = REPLACE(retvalue, COALESCE(SUBSTRING(retvalue
FROM "pattern"), ''), "replacement");
 END LOOP;
 RETURN retvalue;
END;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

The problem was that SUBSTRING returns NULL if it cannot find any
matches for the pattern and when the second parameter to REPLACE
returns NULL, REPLACE returns NULL (which is idiotic). Using COALESCE I
ensure that is SUBSTRING cannot find a match that '' (empty string) is
sent to REPLACE. REPLACE then behaves as expected and replaces nothing.

Debugging PostgreSQL's retarded behaviour around NULL values can be a
real pain. But at least I learned to use EMS PostgreSQL Manager for
Windows' function debugger, which can step through a function while
reporting the values of all variables, parameters and return values.
Very handy.

-Robert


pgsql-general by date:

Previous
From: "rlee0001"
Date:
Subject: Re: Little Offtopic: Database frontends
Next
From: "James Harper"
Date:
Subject: libpq questions