Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

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: Sun, 28 Oct 2007 12:42:47 +0100
  • Message-id: <20071028114247.GA1327@KanotixBox> <text/plain>

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



Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group