pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
trying to pattern match to a value contained in a column
8+ messages / 7 participants
[nested] [flat]

* trying to pattern match to a value contained in a column
@ 2000-12-07 21:05 Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 21:28 ` RE: trying to pattern match to a value contained in a column Francis Solomon <francis@stellison.co.uk>
  2000-12-07 21:39 ` Re: trying to pattern match to a value contained in a column Peter Eisentraut <peter_e@gmx.net>
  0 siblings, 2 replies; 8+ messages in thread

From: Beth Gatewood @ 2000-12-07 21:05 UTC (permalink / raw)
  To: pgsql-sql

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





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* RE: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
@ 2000-12-07 21:28 ` Francis Solomon <francis@stellison.co.uk>
  2000-12-07 21:45   ` Re: trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  1 sibling, 1 reply; 8+ messages in thread

From: Francis Solomon @ 2000-12-07 21:28 UTC (permalink / raw)
  To: Beth Gatewood <bethg@mbt.washington.edu>; 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@postgresql.org
> [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Beth Gatewood
> Sent: 07 December 2000 21:06
> To: pgsql-sql@postgresql.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
>
>
>




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 21:28 ` RE: trying to pattern match to a value contained in a column Francis Solomon <francis@stellison.co.uk>
@ 2000-12-07 21:45   ` Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 23:31     ` Re: trying to pattern match to a value contained in a column Tom Lane <tgl@sss.pgh.pa.us>
  2000-12-07 23:32     ` Re: trying to pattern match to a value contained in a column Ross J. Reedstrom <reedstrm@rice.edu>
  2000-12-08 03:01     ` Re: trying to pattern match to a value contained in a column Joel Burton <jburton@scw.org>
  0 siblings, 3 replies; 8+ messages in thread

From: Beth Gatewood @ 2000-12-07 21:45 UTC (permalink / raw)
  To: Francis Solomon <francis@stellison.co.uk>; +Cc: pgsql-sql

Hi Francis-

Thank you for your rapid and excellent response.

This makes perfect sense...unfortunately it isn't working...

I hope this isn't because I am using 6.3 (yes...I know it is very very
old but this is currently where the data is!)

here is the query:

select * from av34s1 where chromat ~~ ('%' || sample || '%');


ERROR:  parser: syntax error at or near "||"

I have also tried using LIKE....

samething..

NOW..
select * from av34s1 where chromat~sample;

ERROR:  There is no operator '~' for types 'bpchar' and 'bpchar'
        You will either have to retype this query using an explicit
cast,
        or you will have to define the operator using CREATE OPERATOR


Indeed...

Table    = av34s1
+----------------------------------+----------------------------------+-------+

|              Field               |              Type                |
Length|
+----------------------------------+----------------------------------+-------+

| contig                           | char()
|    10 |
| contig_pos                       | char()
|    10 |
| read_pos                         | char()
|    10 |
| chromat                          | char()
|    30 |
| sample                           | char()
|    30 |
| allele1                          | char()
|    10 |
| allele2                          | char()
|    10 |
| ref_pos                          | char()
|    10 |
| ref_sample                       | char()
|    10 |
| tag                              | char()
|    10 |
| source                           | char()
|    10 |
+----------------------------------+----------------------------------+-------+

Thanks for your response...

Beth


Francis Solomon wrote:

> 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@postgresql.org
> > [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Beth Gatewood
> > Sent: 07 December 2000 21:06
> > To: pgsql-sql@postgresql.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
> >
> >
> >




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 21:28 ` RE: trying to pattern match to a value contained in a column Francis Solomon <francis@stellison.co.uk>
  2000-12-07 21:45   ` Re: trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
@ 2000-12-07 23:31     ` Tom Lane <tgl@sss.pgh.pa.us>
  2000-12-09 19:27       ` Re: trying to pattern match to a value contained in a column Bruce Momjian <pgman@candle.pha.pa.us>
  2 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2000-12-07 23:31 UTC (permalink / raw)
  To: Beth Gatewood <bethg@mbt.washington.edu>; +Cc: Francis Solomon <francis@stellison.co.uk>; pgsql-sql

Beth Gatewood <bethg@mbt.washington.edu> writes:
> I hope this isn't because I am using 6.3 (yes...I know it is very very
> old but this is currently where the data is!)

> here is the query:
> select * from av34s1 where chromat ~~ ('%' || sample || '%');
> ERROR:  parser: syntax error at or near "||"

I seem to recall that || (and most other operators) wasn't
considered associative by the grammar way back when.
Try a fully parenthesized expression:

select * from av34s1 where chromat ~~ (('%' || sample) || '%');

			regards, tom lane

PS: And do think about updating soon, hmm?



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 21:28 ` RE: trying to pattern match to a value contained in a column Francis Solomon <francis@stellison.co.uk>
  2000-12-07 21:45   ` Re: trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 23:31     ` Re: trying to pattern match to a value contained in a column Tom Lane <tgl@sss.pgh.pa.us>
@ 2000-12-09 19:27       ` Bruce Momjian <pgman@candle.pha.pa.us>
  0 siblings, 0 replies; 8+ messages in thread

From: Bruce Momjian @ 2000-12-09 19:27 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Beth Gatewood <bethg@mbt.washington.edu>; Francis Solomon <francis@stellison.co.uk>; pgsql-sql

> Beth Gatewood <bethg@mbt.washington.edu> writes:
> > I hope this isn't because I am using 6.3 (yes...I know it is very very
> > old but this is currently where the data is!)
> 
> > here is the query:
> > select * from av34s1 where chromat ~~ ('%' || sample || '%');
> > ERROR:  parser: syntax error at or near "||"
> 
> I seem to recall that || (and most other operators) wasn't
> considered associative by the grammar way back when.
> Try a fully parenthesized expression:

Yes || had strange problems in those releases.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 21:28 ` RE: trying to pattern match to a value contained in a column Francis Solomon <francis@stellison.co.uk>
  2000-12-07 21:45   ` Re: trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
@ 2000-12-07 23:32     ` Ross J. Reedstrom <reedstrm@rice.edu>
  2 siblings, 0 replies; 8+ messages in thread

From: Ross J. Reedstrom @ 2000-12-07 23:32 UTC (permalink / raw)
  To: Beth Gatewood <bethg@mbt.washington.edu>; +Cc: Francis Solomon <francis@stellison.co.uk>; pgsql-sql

Beth - 
Both errors you describe are due to using 6.3. The first one might work if
you parenthize the repeated use of ||, as so:

select * from av34s1 where chromat ~~ (('%' || sample ) || '%');

Ross

On Thu, Dec 07, 2000 at 01:45:00PM -0800, Beth Gatewood wrote:
> Hi Francis-
> 
> Thank you for your rapid and excellent response.
> 
> This makes perfect sense...unfortunately it isn't working...
> 
> I hope this isn't because I am using 6.3 (yes...I know it is very very
> old but this is currently where the data is!)
> 
> here is the query:
> 
> select * from av34s1 where chromat ~~ ('%' || sample || '%');
> 
> 
> ERROR:  parser: syntax error at or near "||"
> 
> I have also tried using LIKE....
> 
-- 
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu> 
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St.,  Houston, TX 77005



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
  2000-12-07 21:28 ` RE: trying to pattern match to a value contained in a column Francis Solomon <francis@stellison.co.uk>
  2000-12-07 21:45   ` Re: trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
@ 2000-12-08 03:01     ` Joel Burton <jburton@scw.org>
  2 siblings, 0 replies; 8+ messages in thread

From: Joel Burton @ 2000-12-08 03:01 UTC (permalink / raw)
  To: Beth Gatewood <bethg@mbt.washington.edu>; +Cc: pgsql-sql

> This makes perfect sense...unfortunately it isn't working...
> 
> I hope this isn't because I am using 6.3 (yes...I know it is very very
> old but this is currently where the data is!)
> 
> here is the query:
> 
> select * from av34s1 where chromat ~~ ('%' || sample || '%');
> 
> 
> ERROR:  parser: syntax error at or near "||"
> 
> I have also tried using LIKE....
> 
> samething..
> 
> NOW..
> select * from av34s1 where chromat~sample;
> 
> ERROR:  There is no operator '~' for types 'bpchar' and 'bpchar'
>         You will either have to retype this query using an explicit
> cast,
>         or you will have to define the operator using CREATE OPERATOR
> 

The suggestion works fine w/recent versions; perhaps it's a '6.3 
thing'

Perhaps

SELECT * FROM tbl WHERE chromat::TEXT ~~ ('%' || sample || '%' 
)::TEXT;  

?

Also, upgrading isn't difficult in most cases; you can pg_dumpall and 
upgrade and restore your files. 7.0 has many nice features over the 
6.x series.
--
Joel Burton, Director of Information Systems -*- jburton@scw.org
Support Center of Washington (www.scw.org)



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: trying to pattern match to a value contained in a column
  2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
@ 2000-12-07 21:39 ` Peter Eisentraut <peter_e@gmx.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Peter Eisentraut @ 2000-12-07 21:39 UTC (permalink / raw)
  To: Beth Gatewood <bethg@mbt.washington.edu>; +Cc: pgsql-sql

Beth Gatewood writes:

> 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]%';

SELECT whatever FROM my_table a, my_table b WHERE a.long_name like (b.abbr || '%');

-- 
Peter Eisentraut      peter_e@gmx.net       http://yi.org/peter-e/




^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2000-12-09 19:27 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-12-07 21:05 trying to pattern match to a value contained in a column Beth Gatewood <bethg@mbt.washington.edu>
2000-12-07 21:28 ` Francis Solomon <francis@stellison.co.uk>
2000-12-07 21:45   ` Beth Gatewood <bethg@mbt.washington.edu>
2000-12-07 23:31     ` Tom Lane <tgl@sss.pgh.pa.us>
2000-12-09 19:27       ` Bruce Momjian <pgman@candle.pha.pa.us>
2000-12-07 23:32     ` Ross J. Reedstrom <reedstrm@rice.edu>
2000-12-08 03:01     ` Joel Burton <jburton@scw.org>
2000-12-07 21:39 ` Peter Eisentraut <peter_e@gmx.net>

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