X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id D992CD1B57C for ; Thu, 29 Jan 2004 17:54:34 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 35681-06 for ; Thu, 29 Jan 2004 13:54:04 -0400 (AST) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id 8F52FD1C969 for ; Thu, 29 Jan 2004 13:54:02 -0400 (AST) Received: from [206.19.64.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 795773; Thu, 29 Jan 2004 09:50:53 -0800 Message-ID: <4019486E.7000807@joeconway.com> Date: Thu, 29 Jan 2004 09:52:46 -0800 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Chris Travers Cc: pgsql-sql@postgresql.org Subject: Re: How to retrieve N lines of a text field. References: <010301c3e679$077698b0$5444053d@winxp> In-Reply-To: <010301c3e679$077698b0$5444053d@winxp> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/270 X-Sequence-Number: 16663 Chris Travers wrote: > This is a complex issue, and i am tryign to figure out how to use regular > expressions to resolve this issue. I need to retrieve the first N lines of > a text field. N would be assigned using a parameterized query, if possible. How 'bout something like this: CREATE OR REPLACE FUNCTION first_n_lines(text, int) RETURNS setof text AS ' DECLARE i int := 0; oneline text; BEGIN LOOP i := i + 1; IF i > $2 THEN EXIT; END IF; SELECT INTO oneline split_part($1, ''\n'', i); IF oneline = '''' THEN EXIT; END IF; RETURN NEXT oneline; END LOOP; RETURN; END ' LANGUAGE 'plpgsql'; regression=# select * from first_n_lines('abc\ndef\nghi', 2); first_n_lines --------------- abc def (2 rows) HTH, Joe