Re: Removing whitespace using regexp_replace

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Removing whitespace using regexp_replace
Date: 2007-10-28 12:32:54
Message-ID: 20071028123254.GB17410@KanotixBox
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thomas Kellerer <spam_eater(at)gmx(dot)net> 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°

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2007-10-28 14:35:59 Re: Removing whitespace using regexp_replace
Previous Message Thomas Kellerer 2007-10-28 12:15:20 Re: Removing whitespace using regexp_replace