pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Steve Frampton <frampton@LinuxNinja.com>
To: pgsql-sql@postgresql.org
Subject: Re: Doing a regexp-based search/replace?
Date: Thu, 18 Oct 2001 15:09:54 -0700
Message-ID: <web-150709@davinci.ethosmedia.com> (raw)
In-Reply-To: <Pine.LNX.4.33.0110181156220.29176-100000@astro.phpwebhosting.com>

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



view thread (5+ messages)

Message-ID: <web-150709@davinci.ethosmedia.com>
Permalink:  ../web-150709@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-150709@davinci.ethosmedia.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, frampton@LinuxNinja.com
  Subject: Re: Doing a regexp-based search/replace?
  In-Reply-To: <web-150709@davinci.ethosmedia.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox