Re: Doing a regexp-based search/replace? - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Doing a regexp-based search/replace?
Date
Msg-id web-150709@davinci.ethosmedia.com
Whole thread Raw
In response to Doing a regexp-based search/replace?  (Steve Frampton <frampton@LinuxNinja.com>)
List 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
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


pgsql-sql by date:

Previous
From: Oleg Lebedev
Date:
Subject: Re: [ADMIN] update in rule
Next
From: Oleg Lebedev
Date:
Subject: Re: COUNT func