Re: How to retrieve N lines of a text field. - Mailing list pgsql-sql

From Joe Conway
Subject Re: How to retrieve N lines of a text field.
Date
Msg-id 4019486E.7000807@joeconway.com
Whole thread Raw
In response to How to retrieve N lines of a text field.  ("Chris Travers" <chris@travelamericas.com>)
List pgsql-sql
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);
IFoneline = '''' 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



pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: query not using index for descending records?
Next
From: Stephan Szabo
Date:
Subject: Re: query not using index for descending records?