Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id BF5A49FA0B7 for ; Sun, 28 Oct 2007 09:33:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 95769-01 for ; Sun, 28 Oct 2007 09:32:52 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from 137086.vserver.de (vs137086.vserver.de [62.75.137.86]) by postgresql.org (Postfix) with ESMTP id 75DA39FA08F for ; Sun, 28 Oct 2007 09:32:57 -0300 (ADT) Received: from a-kretschmer.de (p54B38B9E.dip0.t-ipconnect.de [84.179.139.158]) (authenticated bits=0) by 137086.vserver.de (8.12.8/8.12.8) with ESMTP id l9SCWtBE022111 for ; Sun, 28 Oct 2007 13:32:55 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 4.61) (envelope-from ) id 1Im7K2-00065B-Ph for pgsql-sql@postgresql.org; Sun, 28 Oct 2007 13:32:54 +0100 Date: Sun, 28 Oct 2007 13:32:54 +0100 From: Andreas Kretschmer To: pgsql-sql@postgresql.org Subject: Re: Removing whitespace using regexp_replace Message-ID: <20071028123254.GB17410@KanotixBox> References: <20071028114247.GA1327@KanotixBox> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: mutt-ng 1.5.9i (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/138 X-Sequence-Number: 29510 Thomas Kellerer schrieb: > 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? Oh, yes. > > > >For regexp_replace() you need an extra parameter 'g' like below: > Cool, works like a charm. Nice, i'm glad to help you. > But it seems my problem was actually caused by something else: > > SELECT regexp_replace(myfield, '\s*', '', 'g') > FROM mytable; you should escape the \, change to ...'\\s*'... But without anchors this replaces all whitespaces, also within the text and not only at the beginning/end (^ and $) Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknow) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°