Re: Remove Duplicate Words from a field

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Sukuchha Shrestha <sukuchha(at)yahoo(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Remove Duplicate Words from a field
Date: 2011-05-16 13:06:14
Message-ID: BANLkTikVwLSUe_fwEo4sGvSht2j2HeO63w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello

2011/5/16 Sukuchha Shrestha <sukuchha(at)yahoo(dot)de>:
> Dear All,
> I am new to Postgresql. I have a field with lots of  dublicate words and i
> want to remove any dublicate word from that field.
> For example, if i have a field with a string " one, one, two, two, three",
> how would i get " one, two, three" only ?
> Any help is much apprecieated !
> Sukuchha

postgres=# CREATE OR REPLACE FUNCTION uniq_worlds(text)
RETURNS text AS $$
SELECT array_to_string(ARRAY(SELECT DISTINCT trim(x) FROM
unnest(string_to_array($1,',')) x),', ')
$$ LANGUAGE sql;
CREATE FUNCTION
Time: 14.281 ms
postgres=# SELECT uniq_worlds('one, two, three, one');
uniq_worlds
─────────────────
three, two, one
(1 row)

Time: 0.817 ms

Regards

Pavel Stehule

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sukuchha Shrestha 2011-05-16 13:40:01 Re: Remove Duplicate Words from a field
Previous Message Kalai R 2011-05-16 13:04:43 Postgre Installation Problem