Re: [GENERAL] workaround for lack of REPLACE() function

From: Joe Conway <mail(at)joeconway(dot)com>
To: Thomas Lockhart <lockhart(at)fourpalms(dot)org>
Cc: Agent155 Support <matt(at)planetnet(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers List <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [GENERAL] workaround for lack of REPLACE() function
Date: 2002-07-12 17:47:54
Message-ID: 3D2F164A.7000205@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-patches

Thomas Lockhart wrote:
> (crossposted to -hackers, should follow up on that list)

<snip>

> OK, this is in the "can't do it what we have" category. Should we have
> it accept a regular expression rather than a simple string? In either
> case it should probably go into the main distro. Except that I see
> "REPLACE" is mentioned as a reserved word in SQL99. But has no other
> mention in my copy of the draft standard. Anyone else have an idea what
> it might be used for in the standard?

Not sure, but I see what you mean. Perhaps because of Oracle pushing to
legitimize the "CREATE OR REPLACE" syntax? In any case, this works in 8i:

SQL> select replace('hello','l','x') from dual;

REPLACE('HELLO','L','X')
------------------------
hexxo

and here it is in MSSQL 7:

select replace('hello','l','x')
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

hexxo

(1 row(s) affected)

and my proposed PostgreSQL function:

test=# select replace('hello','l','x');
replace
---------
hexxo
(1 row)

so at least we would be consistant/compatable with these two.

>
> The other functions look useful too, unless to_char() and varbit can be
> evolved to support this functionality.

I will take a look at merging these into existing functions, but I have
a few other things ahead of this in my queue.

One of the reasons I wasn't pushing too hard to get replace() into the
backend is because my current solution is a bit of a hack. It uses the
builtin length, strpos and substr text functions (which I think makes
sense since they already know how to deal with mb strings), but because
they accept and return text, I'm doing lots of conversions back and
forth from (* text) to (* char). To do this "right" probably means
reworking the text string manipulation functions to be wrappers around
some equivalent functions accepting and returning C strings. That was
more work than I had time for when I wrote the current replace(). But as
I said, if there is support for getting this into the backend, I'll add
it to my todo list:

- Create new backend function replace()
- Either create new backend functions, or merge into existing functions:
to_hex() and extract_tok()

Joe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rune Teigen 2002-07-12 17:54:16 PostgreSQL in mission-critical system
Previous Message Tom Ince 2002-07-12 17:47:38 Re: ODBC Error while selecting a numeric data field

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2002-07-12 18:43:20 pgsql/ oc/src/sgml/catalogs.sgml oc/src/sgml/r ...
Previous Message Tom Lane 2002-07-12 17:14:56 Re: test data for query optimizer

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2002-07-13 01:38:47 Re: Prepare xlog for optional oid
Previous Message Thomas Lockhart 2002-07-12 15:07:33 Re: workaround for lack of REPLACE() function