RE: trying to pattern match to a value contained in a column

From: "Francis Solomon" <francis(at)stellison(dot)co(dot)uk>
To: "Beth Gatewood" <bethg(at)mbt(dot)washington(dot)edu>, <pgsql-sql(at)postgresql(dot)org>
Subject: RE: trying to pattern match to a value contained in a column
Date: 2000-12-07 21:28:55
Message-ID: NEBBIFFPELJMCJAODNPKIEKHCDAA.francis@stellison.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi Beth,

Try something like this ...

Here's a simple table schema:

CREATE TABLE abbrev (
abbr varchar(10),
long_name varchar(50),
primary key(abbr)
);

Throw in some random data:

INSERT INTO abbrev VALUES ('fs', 'fsolomon');
INSERT INTO abbrev VALUES ('bg', 'bgatewood');
INSERT INTO abbrev VALUES ('junk', 'nomatch');

Query the table:

SELECT * FROM abbrev WHERE long_name~abbr;

... which yields these results:

abbr | long_name
------+-----------
fs | fsolomon
bg | bgatewood

Note that ~ does a case-sensitive regex match. If you really want a
'like' match, you could do this instead:

SELECT * FROM abbrev where long_name~~('%' || abbr || '%');

... where '||' is the string-concatenation operator.

Hope this helps

Francis Solomon

> -----Original Message-----
> From: pgsql-sql-owner(at)postgresql(dot)org
> [mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of Beth Gatewood
> Sent: 07 December 2000 21:06
> To: pgsql-sql(at)postgresql(dot)org
> Subject: [SQL] trying to pattern match to a value contained
> in a column
>
>
> Hi-
>
> I can't figure out how to do this....
>
> I examine a table where I think that one attribute is an
> abbreviation of
> another attribute.
>
> So-If I had a table where I had LONG_NAME and ABBR as attributes.
>
> I want something like
>
> SELECT whatever FROM my_table WHERE long_name LIKE '%[the
> value of ABBR
> in that row]%';
>
>
> Of course this doesn't work...
>
> Any thoughts?
>
> Thanks-
> Beth
>
>
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 2000-12-07 21:39:07 Re: trying to pattern match to a value contained in a column
Previous Message Beth Gatewood 2000-12-07 21:05:56 trying to pattern match to a value contained in a column