From: | Thomas Lockhart <lockhart(at)fourpalms(dot)org> |
---|---|
To: | Joe Conway <mail(at)joeconway(dot)com> |
Cc: | Agent155 Support <matt(at)planetnet(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general(at)postgresql(dot)org, PostgreSQL Hackers List <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: workaround for lack of REPLACE() function |
Date: | 2002-07-12 15:07:33 |
Message-ID: | 3D2EF0B5.1B67D437@fourpalms.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general pgsql-hackers pgsql-patches |
(crossposted to -hackers, should follow up on that list)
> Well, OVERLAY is defined as:
> overlay(string placing string from integer [for integer])
> and replace() is defined (by me at least) as:
> replace(inputstring, old-substr, new-substr)
OK.
> OVERLAY requires that I know the "from" position and possibly the "for"
> in advance. Other functions (such as strpos() and substr()) can be used
> to help...
Right. So you can do your example pretty easily:
thomas=# select overlay(f1 placing '' from position('/local' in f1)
thomas-# for length('/local')) from strtest;
overlay
--------------------
/usr/pgsql/data
/m1/usr/pgsql/data
And if you don't like that much typing you can do:
thomas=# create function replace(text, text, text) returns text as '
thomas'# select overlay($1 placing $3 from position($2 in $1) for
length($2));
thomas'# ' language 'sql';
CREATE FUNCTION
thomas=# select replace(f1, '/local', '') from strtest;
replace
--------------------
/usr/pgsql/data
/m1/usr/pgsql/data
> But now what happens if you wanted to replace all of the '/' characters
> with '\'?...
> You can't do this at all with overlay(), unless you want to write a
> PL/pgSQL function and loop through each string. I started out with
> exactly this, using strpos() and substr(), but I thought a C function
> was cleaner, and it is certainly faster.
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?
The other functions look useful too, unless to_char() and varbit can be
evolved to support this functionality.
- Thomas
From | Date | Subject | |
---|---|---|---|
Next Message | Johnson, Shaunn | 2002-07-12 15:23:45 | Time table was created / admin clean-up |
Previous Message | Jean-Luc Lachance | 2002-07-12 14:45:13 | Re: 7.2.1 optimises very badly against 7.2 |
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2002-07-12 15:14:05 | Re: Bug of PL/pgSQL parser |
Previous Message | Bruce Momjian | 2002-07-12 14:26:11 | Re: [PATCHES] Changes in /contrib/fulltextindex |
From | Date | Subject | |
---|---|---|---|
Next Message | Joe Conway | 2002-07-12 17:47:54 | Re: [GENERAL] workaround for lack of REPLACE() function |
Previous Message | Manfred Koizar | 2002-07-12 08:31:55 | Re: Prepare xlog for optional oid |