Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id B22849FB527 for ; Sun, 28 Oct 2007 09:15:45 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 84259-07 for ; Sun, 28 Oct 2007 09:15:42 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id 35C679FB4D2 for ; Sun, 28 Oct 2007 09:15:42 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1Im73F-0003PV-93 for pgsql-sql@postgresql.org; Sun, 28 Oct 2007 12:15:33 +0000 Received: from ppp-82-135-65-251.dynamic.mnet-online.de ([82.135.65.251]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 28 Oct 2007 12:15:33 +0000 Received: from spam_eater by ppp-82-135-65-251.dynamic.mnet-online.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 28 Oct 2007 12:15:33 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: Removing whitespace using regexp_replace Date: Sun, 28 Oct 2007 13:15:20 +0100 Lines: 43 Message-ID: References: <20071028114247.GA1327@KanotixBox> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ppp-82-135-65-251.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.6) Gecko/20070728 Thunderbird/2.0.0.6 Mnenhy/0.7.5.666 In-Reply-To: <20071028114247.GA1327@KanotixBox> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/137 X-Sequence-Number: 29509 Andreas Kretschmer wrote on 28.10.2007 12:42: >> I have a column with the datatype "text" that may contain leading >> whitespace (tabs, spaces newlines, ...) and I would like to remove them all >> (ideally leading and trailing). > > You can use trim() for that: > > select 'x' || trim(both '\t' from trim(both ' ' from ' \t\tfoo bar ')) || 'x'; > > (for testing with 'x' around the result) Yes I was thinking about a solution like that as well, but wouldn't that only work if the order in which spaces and tabs appear is always the same? The above would replace ' \t' but not '\t ', right? > For regexp_replace() you need an extra parameter 'g' like below: Cool, works like a charm. Didn't see that parameter when first reading that chapter. But it seems my problem was actually caused by something else: SELECT regexp_replace(myfield, '\s*', '', 'g') FROM mytable; does not replace anything, but SELECT regexp_replace(myfield, '[ \t\n\r]*', '', 'g') FROM mytable; does replace all whitespaces (as I expected). And subsequently SELECT regexp_replace(myfield, '^[ \t\n\r]*', '', 'g') FROM mytable; replaces only the whitespace at the beginning. I thought \s is a "shortcut" for "whitespace", which in my understanding is the same as [ \t\r\n]. Am I wrong here? Cheers Thomas