Re: Querying for strings that match after prefix

From: Joseph Brenner <doom(at)kzsu(dot)stanford(dot)edu>
To: "badlydrawnbhoy" <badlydrawnbhoy(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Querying for strings that match after prefix
Date: 2006-06-02 19:43:17
Message-ID: 200606021943.k52JhI392764@mail0.rawbw.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


badlydrawnbhoy <badlydrawnbhoy(at)gmail(dot)com> wrote:

> I hope this is the right forum for this, but please correct me if
> somewhere else is more appropriate.
>
> I need to locate all the entries in a table that match , but only after
> a number of characters have been ignored. I have a table of email
> addresses, and someone else has erroneously entered some addresses
> prefixed with 'mailto:', which I'd like to ignore.
>
> An example would be: john(dot)smith(at)smiths(dot)com should match
> mailto:john(dot)smith(at)smiths(dot)com
>
> I've tried the following
>
> select address
> from people
> where address = (select replace(address, 'mailto:', '') from people);
>
> which gives me the error
>
> ERROR: more than one row returned by a subquery used as an expression

There's no need to use a sub-select for this, this should do the job:

SELECT REPLACE(address, 'mailto:', '') FROM people;

You also have some options for "fuzzy" matching in the WHERE clause, e.g.

SELECT address FROM people WHERE address LIKE '%doom(at)%'

Will find all email addresses like "doom(at)(dot)(dot)(dot)", whether or not there's a
'mailto:' prefix. (% matches any character string).

This will find all the records with the erroneous "mailto:" prefix:

SELECT address FROM people WHERE address LIKE 'mailto:%'

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joseph Brenner 2006-06-02 19:46:24 How to do a "CREATE DATABASE" and then connect to it?
Previous Message Rich Shepard 2006-06-02 19:17:31 Re: Best open source tool for database design / ERDs?