Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 022152E0056 for ; Thu, 24 Apr 2008 13:19:57 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 87453-02 for ; Thu, 24 Apr 2008 13:19:53 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by developer.postgresql.org (Postfix) with ESMTP id 22A832E0042 for ; Thu, 24 Apr 2008 13:19:53 -0300 (ADT) Received: from [62.69.60.39] (helo=localhost) by zebedee.cranntara.net with esmtpsa (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.62) (envelope-from ) id 1Jp4Al-0000ah-Ix; Thu, 24 Apr 2008 17:19:49 +0100 Received: from [192.168.0.32] (helo=dick.coachhouse ident=tarlika) by localhost with esmtp (Exim 4.63) (envelope-from ) id 1Jp43t-0004KI-Rk; Thu, 24 Apr 2008 17:12:41 +0100 Date: Thu, 24 Apr 2008 17:12:41 +0100 From: Tarlika Elisabeth Schmitz To: "A. Kretschmer" Cc: pgsql-sql@postgresql.org Subject: Re: extracting words Message-ID: <20080424171241.60b030c0@dick.coachhouse> In-Reply-To: <20080423051842.GA8401@a-kretschmer.de> References: <20080422222504.56bdb06d@dick.coachhouse> <20080423051842.GA8401@a-kretschmer.de> X-Mailer: Claws Mail 3.2.0 (GTK+ 2.8.20; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200804/172 X-Sequence-Number: 30711 On Wed, 23 Apr 2008 07:18:44 +0200 "A. Kretschmer" wrote: > am Tue, dem 22.04.2008, um 22:25:04 +0100 mailte Tarlika Elisabeth > Schmitz folgendes: > > TABLE product: product_pk, product_name, department_fk > > TABLE product: department_pk, department_name > > > > ... > > > > > > I need to extract the words from department_name and product_name > > (words are separated by spaces) ... > > A little function (author: David Fetter) > > -- split a string to rows, by David Fetter > CREATE OR REPLACE FUNCTION split_to_rows(TEXT, TEXT) RETURNS SETOF > TEXT STRICT > LANGUAGE sql > AS $$ > SELECT (string_to_array($1, $2))[s.i] > FROM generate_series( > 1, > array_upper(string_to_array($1, $2), 1) > ) AS s(i); > $$; > > > And now: > > test=*# select * from product ; > id | val1 | val2 > ----+------------------+------------------- > 1 | cakes & desserts | apple crumble > 2 | cakes & desserts | cheese cake > 3 | starters & soups | french onion soup > (3 rows) > > test=*# select id, split_to_rows(replace(val1 || ' ' || val2,' > &',''),' ') as col1 from product; id | col1 > ----+---------- > 1 | cakes > 1 | desserts > 1 | apple > 1 | crumble > 2 | cakes > 2 | desserts > 2 | cheese > 2 | cake > 3 | starters > 3 | soups > 3 | french > 3 | onion > 3 | soup > (13 rows) Many thanks! This is ingenious! Before I ask any stupid questions such as "what does replace (text, ' &', '') do?": where can I find a more detailed description of the string functions? I would like to treat all white space (new-line, tab, space) as word separator. Will the above work on 7.4? -- Best Regards, Tarlika Elisabeth Schmitz A: Because it breaks the logical sequence of discussion Q: Why is top posting bad?