Skip site navigation (1) Skip section navigation (2)

Cool PL/PgSQL hack :)

From: David Fetter <david(at)fetter(dot)org>
To: SF Postgres <sfpug(at)postgresql(dot)org>
Subject: Cool PL/PgSQL hack :)
Date: 2003-05-29 21:59:47
Message-ID: 20030529215947.GA17648@fetter.org (view raw or flat)
Thread:
Lists: sfpug
Kind people,

Here's a little hack I put together that looks a lot like Perl's
join() operator.  Comments, suggestions for improvement, and sources
for old-school pizzelle irons are all welcome :)

Cheers,
D

CREATE OR REPLACE FUNCTION string_join(VARCHAR, VARCHAR[])
RETURNS VARCHAR AS '
DECLARE
  joiner    ALIAS FOR $1;
  my_array  ALIAS FOR $2;
  dimstring TEXT;
  joined_stuff TEXT;
  counter   INTEGER;
  the_start INTEGER;
  the_end   INTEGER;
BEGIN
    SELECT INTO dimstring array_dims(my_array);
    the_start := ltrim(split_part(dimstring, '':'', 1), ''['')::INTEGER;
    the_end   := rtrim(split_part(dimstring, '':'', 2), '']'')::INTEGER;
    FOR counter IN the_start .. the_end LOOP
        IF counter = 1
        THEN
            joined_stuff := my_array[counter];
        ELSE
            joined_stuff := joined_stuff || joiner || my_array[counter];
        END IF;
    END LOOP;
    RETURN (joined_stuff);
END;
' LANGUAGE 'plpgsql';

-- 
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100    cell: +1 415 235 3778

Responses

sfpug by date

Next:From: Stephan SzaboDate: 2003-05-29 22:06:00
Subject: Re: Next Meeting Set ... June 25th
Previous:From: Josh BerkusDate: 2003-05-29 21:24:09
Subject: Re: Next Meeting Set ... June 25th

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group