pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Kretschmer <akretschmer@spamfence.net>
To: pgsql-sql@postgresql.org
Subject: Re: Removing whitespace using regexp_replace
Date: Sun, 28 Oct 2007 12:42:47 +0100
Message-ID: <20071028114247.GA1327@KanotixBox> (raw)
In-Reply-To: <fg1ra3$ehj$1@ger.gmane.org>
References: <fg1ra3$ehj$1@ger.gmane.org>

Thomas Kellerer <spam_eater@gmx.net> schrieb:

> Hi,
> 
> 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)


> 
> I tried
> 
> SELECT regexp_replace(myfield, '\A\s*', '')
> FROM mytable;

For regexp_replace() you need an extra parameter 'g' like below:

test=*# select 'x' || regexp_replace(regexp_replace(' \t\tfoo bar  ', '^[ \t]+','','g'),'[ \t]+$','','g') || 'x';
                                                                                   ^^^               ^^^


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°



view thread (5+ messages)  latest in thread

Message-ID: <20071028114247.GA1327@KanotixBox>
Permalink:  ../20071028114247.GA1327@KanotixBox/
Also on:    postgresql.org/message-id/20071028114247.GA1327@KanotixBox

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: akretschmer@spamfence.net
  Subject: Re: Removing whitespace using regexp_replace
  In-Reply-To: <20071028114247.GA1327@KanotixBox>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox