Received: from frithjof.reiner (b88.dialin.tuwien.ac.at [192.35.240.218]) by postgresql.org (8.11.3/8.11.1) with SMTP id f3CG0m821327 for ; Thu, 12 Apr 2001 12:00:49 -0400 (EDT) (envelope-from areiner@tph.tuwien.ac.at) Received: (qmail 454 invoked by uid 500); 12 Apr 2001 15:43:38 -0000 From: "Albert REINER" Date: Thu, 12 Apr 2001 17:43:37 +0200 To: pgsql-sql@postgresql.org Subject: Re: RE: Re: select substr??? Message-ID: <20010412174337.B253@frithjof> Mail-Followup-To: pgsql-sql@postgresql.org References: <08CD1781F85AD4118E0800A0C9B8580B094A0C@NEZU> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <08CD1781F85AD4118E0800A0C9B8580B094A0C@NEZU>; from jeckermann@verio.net on Tue, Apr 10, 2001 at 05:41:26PM -0500 X-Archive-Number: 200104/177 X-Sequence-Number: 2525 Thanks, interesting. I did not find any mention of regular expressions in the 7.0.2 docs I installed locally. BTW, your code does not do exactly the same, as it removes any whitespace while the other one only collapses consecutive blanks. But, of course, regular expressions in PL/pgSQL make this much easier. As a further aside, I think that in both versions of the function the check for `$1 IS NULL' is not necessary; I got the impression that passing NULL as an argument to a function will automatically return NULL as the result, doesn't it? Albert. On Tue, Apr 10, 2001 at 05:41:26PM -0500, Jeff Eckermann wrote: > Regular expressions make this much easier. The below could be shortened to: > > create function ComparisonString(text) returns text as ' > declare > t alias for $1; > r text; > c char; > begin > if t is null or t !~ ''[^a-zA-Z0-9]'' > then > return t; > end if; > r = ''''; > for i in 1 .. char_length(t) loop > c = substring(t from i for 1); > if c ~ ''[a-zA-Z0-9]'' > then > r = r || c; > end if; > end loop; > return r; > end; > ' language 'plpgsql' with (IsCachable); > > > -----Original Message----- > > From: Albert REINER [SMTP:areiner@tph.tuwien.ac.at] ... > > > > somewhere I use the following, which might be adapted to do what you > > want. I am sure there are more elegant ways of doing this, though. > > > > create function ComparisonString(text) returns text as ' > > declare > > t text; > > r text; > > c char; > > ns bool; > > begin > > if $1 is null then > > return NULL; > > end if; > > t = lower(trim(both $1)); > > r = ''''; > > ns = false; > > for i in 1 .. char_length(t) loop > > c = substring(t from i for 1); > > if c = '' '' then > > if ns then > > r = r || '' ''; > > end if; > > ns = false; > > else > > if position(c in ''abcdefghijklmnopqrstuvwxyz0123456789'') > 0 > > then > > r = r || c; > > ns = true; > > end if; > > end if; > > end loop; > > return trim(both r); > > end; > > ' language 'plpgsql' with (IsCachable); > >