strpos NOT doing what I'd expect

From: Ralph Smith <smithrn(at)washington(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: strpos NOT doing what I'd expect
Date: 2008-06-07 00:58:28
Message-ID: 37C882AE-F4C4-4770-86D9-C4219694BDF7@washington.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

CODE:
===============================
CREATE OR REPLACE FUNCTION find_next_delim(invar varchar, delimlist
varchar) RETURNS integer AS
$$

/* OVERLOADED Function. The other version takes a 3rd parameter as
the
starting position in invar.
*/

DECLARE

achar character := '' ;
j int := 0 ;

BEGIN

IF length(delimlist) = 0 THEN
RAISE NOTICE 'In function \'find_next_delim\' the delimiter
cannot be null.' ;
END IF ;

FOR i IN 1 .. length(invar)
LOOP

j := j + 1 ;
achar := substring(invar from i for 1 ) ;
RAISE NOTICE 'achar is R%S',achar ;
IF strpos(delimlist,achar) <> 0 THEN
RETURN j ;
END IF ;

END LOOP ;

RETURN 0 ;

END ;
$$ LANGUAGE plpgsql ; /* find_next_delim */

WHAT'S HAPPENING:
===============================
airburst=# select find_next_delim('ralph smith','3') ;

NOTICE: achar is RrS
NOTICE: achar is RaS
NOTICE: achar is RlS
NOTICE: achar is RpS
NOTICE: achar is RhS
NOTICE: achar is R S
find_next_delim
-----------------
6
(1 row)

airburst=# select find_next_delim('ralph smith','') ; -- for the heck
of it, that's a null

NOTICE: In function 'find_next_delim' the delimiter cannot be null.
NOTICE: achar is RrS
NOTICE: achar is RaS
NOTICE: achar is RlS
NOTICE: achar is RpS
NOTICE: achar is RhS
NOTICE: achar is R S
find_next_delim
-----------------
6
(1 row)

WHY find a match on the space???

Thanks!

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-06-07 01:47:37 Re: PL/pgSQL graph enumeration function hangs
Previous Message Celso Pinto 2008-06-07 00:05:08 array column and b-tree index allowing only 8191 bytes