Re: Doing a regexp-based search/replace?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Steve Frampton <frampton(at)LinuxNinja(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Doing a regexp-based search/replace?
Date: 2001-10-18 19:25:14
Message-ID: Pine.BSF.4.21.0110181221150.27862-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql


Well, the easiest general way is probably a plperl function, but I think
the following may work for your specific case:

update mytable set
property_id=substr(property_id, 1, position('-' in property_id)-1)
where position('-' in property_id)!=0;

On Thu, 18 Oct 2001, Steve Frampton wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello:
>
> I've got a table containing property_id's with values of the form
> ###-####. I would like to discard the slash onwards (and I can't use a
> substr() because I am not guaranteed if a) the -#### portion exists, b)
> what position it exists from.
>
> If this were a text file, I would use a sed expression such as:
>
> cat textfile | sed 's/-.*$//'
>
> I've been looking for a way to do this with PostgreSQL but so far haven't
> found a function that seems to be suitable. I thought maybe I could do it
> with translate, but translate doesn't appear to work with regular
> expressions. So far I've tried things like:
>
> select translate(property_id, '-.*', '') from mytable;
>
> I need to do this, because the -.* portion of my property_id was entered
> in error, and I would like to do an update on the entire table and just
> have the left-hand side of the property_id column remaining.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Francisco Solari 2001-10-18 19:49:09 Re: "Can't Find any tables, sequences or indexes!" Message
Previous Message Stephan Szabo 2001-10-18 19:14:08 Re: update in rule

Browse pgsql-sql by date

  From Date Subject
Next Message Oleg Lebedev 2001-10-18 19:37:17 COUNT func
Previous Message Stephan Szabo 2001-10-18 19:14:08 Re: update in rule