Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f9IM9wP56955 for ; Thu, 18 Oct 2001 18:09:58 -0400 (EDT) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.1) with HTTP id 150709; Thu, 18 Oct 2001 15:09:54 -0700 From: "Josh Berkus" Subject: Re: Doing a regexp-based search/replace? To: Steve Frampton , X-Mailer: CommuniGate Pro Web Mailer v.3.4.1 Date: Thu, 18 Oct 2001 15:09:54 -0700 Message-ID: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200110/262 X-Sequence-Number: 5216 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@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco