Re: How to retrieve N lines of a text field.
| От | Joe Conway |
|---|---|
| Тема | Re: How to retrieve N lines of a text field. |
| Дата | |
| Msg-id | 4019486E.7000807@joeconway.com обсуждение исходный текст |
| Ответ на | How to retrieve N lines of a text field. ("Chris Travers" <chris@travelamericas.com>) |
| Список | 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 по дате отправления: