Re: Doing a regexp-based search/replace?

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Steve Frampton <frampton(at)LinuxNinja(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Doing a regexp-based search/replace?
Date: 2001-10-18 22:09:54
Message-ID: web-150709@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql

Steve,

> 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/-.*$//'

In SQL/plpgsql, you can't do this with a single expression. However,
you can do it with three expressions put together.

CREATE FUNCTION remove_propid_tail (
VARCHAR ) RETURNS VARCHAR AS'
SELECT SUBSTR($1, 1, ((STRPOS($1, ''-'') - 1));
END;'
LANGUAGE 'SQL';

Then run:

UPDATE main_table SET property_id = remove_propid_tail(property_id)
WHERE property_id ~ '-';

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Brian McCane 2001-10-19 00:50:07 Re: delete/vacuum not freeing disk space
Previous Message Peter Eisentraut 2001-10-18 21:06:09 Re: "Can't Find any tables, sequences or indexes!" Message

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2001-10-18 22:39:22 Re: COUNT func
Previous Message Oleg Lebedev 2001-10-18 20:40:00 Re: [ADMIN] update in rule