Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 73095632795 for ; Fri, 25 Jun 2010 19:24:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 34600-05 for ; Fri, 25 Jun 2010 22:23:53 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from blu0-omc2-s6.blu0.hotmail.com (blu0-omc2-s6.blu0.hotmail.com [65.55.111.81]) by mail.postgresql.org (Postfix) with ESMTP id 1660D632621 for ; Fri, 25 Jun 2010 19:23:54 -0300 (ADT) Received: from BLU0-SMTP78 ([65.55.111.73]) by blu0-omc2-s6.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 25 Jun 2010 15:23:53 -0700 X-Originating-IP: [64.231.91.225] X-Originating-Email: [fbax@sympatico.ca] Message-ID: Received: from t60p.bax.ca ([64.231.91.225]) by BLU0-SMTP78.blu0.hotmail.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Fri, 25 Jun 2010 15:23:53 -0700 Date: Fri, 25 Jun 2010 18:23:48 -0400 From: Frank Bax User-Agent: Thunderbird 2.0.0.22 (X11/20090704) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: How do I remove selected words from text field? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 25 Jun 2010 22:23:53.0370 (UTC) FILETIME=[17F417A0:01CB14B5] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.801 tagged_above=-5 required=5 tests=BAYES_50=0.8, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201006/95 X-Sequence-Number: 34286 I'm not quite sure how to ask for the query I want, so let's start with data: create table t1 (i int, val varchar); insert into t1 values(1,'A B C D'); insert into t1 values(2,'B D E F'); insert into t1 values(3,'G H I J'); create table t2 (q varchar, z varchar); insert into t2 values('A','vowel'); insert into t2 values('B','consonant'); insert into t2 values('E','vowel'); insert into t2 values('K','consonant'); t1.val will contain "words" separated by blanks. It might be better if each "word" were a separate row in another table; but that's not how the legacy database was built. I understand this can be simulated by: select i,a[s] from (select i, generate_subscripts(string_to_array(val,' '),1) as s, string_to_array(val,' ') as a from t1) foo; In my "real life" situation, the "words" are not single letters. I'd like to write a function that removes selected "words" from t1.val based on select on t2. In the above example; let's exclude all vowels, so I end up with: 1 'B C D' 2 'B D F' 3 'G H I J' For some "words" in val; there may not be a row when joining to t2.q; these words must be included in final result. In the above example; there is no row in t2 where q="I"; so it is included in result. How do I write such a function? Can it be done with SQL only?