Re: string parsing - Mailing list pgsql-general

From Joe Conway
Subject Re: string parsing
Date
Msg-id 3D99D2EF.4080906@joeconway.com
Whole thread Raw
In response to string parsing  (Jean-Christian Imbeault <jc@mega-bucks.co.jp>)
List pgsql-general
Jean-Christian Imbeault wrote:
> Is there an SQl query that will parse a string on a separator (whites
> pace) and return one word per row in the result set? (I don't know *any*
> perl so I can't write a PL/PGSQL function, and I'm worried that a perl
> function wouldn't be multi-byte safe ...)

In 7.2.x you could create your own C function to do this.

In 7.3beta, you could also create a PL/pgSQL function (note PL/pgSQL is not
the same as PL/Perl). For example:

CREATE OR REPLACE FUNCTION parse_words(text)
RETURNS SETOF text AS '
   DECLARE
     i int := 0;
     word text;
   BEGIN
     LOOP
       i := i + 1;
       SELECT INTO word split_part($1, '' '', i);
       IF word = '''' THEN
         EXIT;
       END IF;
       RETURN NEXT word;
     END LOOP;
     RETURN;
   END
' LANGUAGE 'plpgsql';

select * from parse_words('abc def hij klm');
  parse_words
-------------
  abc
  def
  hij
  klm
(4 rows)

This should be multi-byte safe.

HTH,

Joe


pgsql-general by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Query plan not using index for some reason.
Next
From: Tom Lane
Date:
Subject: Re: Query plan not using index for some reason.