Posted By: DoomBringer | Sep 11th, 2007 @ 3:48 PM
page 1 of 1
Comments: 6 | Views: 7278
DoomBringer
DoomBringer
Doom!

In SQL, I know you can do something like this:

select * from tbl where col1 like %foo%

and it will return any results that have "foo" inside of them, for instance, myfoo would be returned if it was in the table.  What I want to do is a bit the other direction.  Is there an easy way to input "myfoo" and get "foo" out (assuming "foo" is a value in the table)?  More specifically, I want a count of hits, but that is easy enough to add on after the query is there.

Some other notes: col1 is the primary key, so in the end I'm only looking for a 0 or 1.

The end goal is to find out if the inputted parameter, "myfoo" contains a word from the table.  What I was thinking might be possible is using a temporary table to hold "myfoo" and then using a WHERE-IN clause and a nested query:

select * from temptbl where col1 in (select * from tbl)

But then I realized that WHERE-IN probably doesn't support LIKE.

Matthew van Eerde
Matthew van Eerde
AKA Maurits
where 'myfoo' like '%' + col1 + '%'
-- returns rows where col1 is any of:
-- ''
-- m
-- y
-- f
-- o
-- my
-- yf
-- fo
-- oo
-- myf
-- yfo
-- foo
-- myfo
-- yfoo
-- myfoo
Matthew van Eerde
Matthew van Eerde
AKA Maurits
Maybe there's a different operator for concatenating strings in postgreSQL?

Or could you pre-add the %s in the parameter?
Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
DoomBringer wrote:
Actually, I think pgsql does have a different cat operator.  || is the operator for it.  (Weird I know.)

Anyhow, I'll keep this in mind for when I need to do this for Oracle and MSSQL. 


Actually,  I think Oracle uses || as well. Vague recollections.
page 1 of 1
Comments: 6 | Views: 7278