dbTalk Databases Forums  

I need a LIKE operator with IN functionality

comp.databases comp.databases


Discuss I need a LIKE operator with IN functionality in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Quiet Center
 
Posts: n/a

Default I need a LIKE operator with IN functionality - 08-17-2010 , 10:30 AM






Here is what we can do with IN:

SELECT * FROM language WHERE langkey IN ( SELECT acct_desc || '-' FROM
dbo.product WHERE acct_code LIKE 'WSDOM%');

but what if you dont want exact matching like IN gives and want to do
a LIKE... what can you do.. Here is what I want:

SELECT * FROM language WHERE langkey INLIKE ( SELECT acct_desc || '-%'
FROM dbo.product WHERE acct_code LIKE 'WSDOM%');

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-17-2010 , 11:59 AM






On 2010-08-17 17:30, The Quiet Center wrote:
Quote:
Here is what we can do with IN:

SELECT * FROM language WHERE langkey IN ( SELECT acct_desc || '-' FROM
dbo.product WHERE acct_code LIKE 'WSDOM%');

but what if you dont want exact matching like IN gives and want to do
a LIKE... what can you do.. Here is what I want:

SELECT * FROM language WHERE langkey INLIKE ( SELECT acct_desc || '-%'
FROM dbo.product WHERE acct_code LIKE 'WSDOM%');
For future questions, you are likely to get more help if you provide
table definitions and some sample data.

create table language (
langkey varchar(20) not null primary key
);

create table product (
acct_code varchar(20) not null primary key,
acct_desc varchar(20) not null
);

insert into product (acct_code, acct_desc)
values ('WSDOM1', 'A'), ('WSDOM21', 'B');

insert into language (langkey)
values ('ALFA'), ('BETA'), ('GAMMA');

select l.* from language l
join product p
on substr(l.langkey,1,length(p.acct_desc)) = p.acct_desc
where p.acct_code like 'WSDOM%';

LANGKEY
--------------------
ALFA
BETA

The substr function may have a different syntax in your dbms, but there
should be something similar that can be used.

You might want to look into your table design to see if it can be improved.


HTH
/Lennart

Reply With Quote
  #3  
Old   
metaperl
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-17-2010 , 02:52 PM



On Aug 17, 12:59*pm, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:

Quote:
For future questions, you are likely to get more help if you provide
table definitions and some sample data.
acknowledged

Quote:
select l.* from language l
join product p
* * on substr(l.langkey,1,length(p.acct_desc)) = p.acct_desc
where p.acct_code like 'WSDOM%';

wow, thanks.

Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-18-2010 , 11:59 AM



CREATE TABLE Patterns (search_pattern VARCHAR(25) NOT NULL);
INSERT INTO Patterns VALUES ('AB%'), (BC%'), etc.


SELECT S.some_column, P.pattern, ..
FROM Source AS S, Patterns AS P
WHERE S.some_column LIKE P.pattern;

You can also use a table constructor directly in the query.

(SELECT pattern
FROM (VALUES ('AB%'), (BC%'), etc.) AS X(pattern))
AS P

Reply With Quote
  #5  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-18-2010 , 12:08 PM



--CELKO-- wrote on 18.08.2010 18:59:
Quote:
CREATE TABLE Patterns (search_pattern VARCHAR(25) NOT NULL);
INSERT INTO Patterns VALUES ('AB%'), (BC%'), etc.


SELECT S.some_column, P.pattern, ..
FROM Source AS S, Patterns AS P
WHERE S.some_column LIKE P.pattern;

You can also use a table constructor directly in the query.

(SELECT pattern
FROM (VALUES ('AB%'), (BC%'), etc.) AS X(pattern))
AS P

Nice trick

Although it won't help the OP because he/she is using SQL Server (as far as I can tell) and last time I checked, SQL Server did not support the row/table constructor using the VALUES clause

Regards
Thomas

Reply With Quote
  #6  
Old   
Jasen Betts
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-20-2010 , 06:56 AM



On 2010-08-17, The Quiet Center <thequietcenter (AT) gmail (DOT) com> wrote:
Quote:
Here is what we can do with IN:

SELECT * FROM language WHERE langkey IN ( SELECT acct_desc || '-' FROM
dbo.product WHERE acct_code LIKE 'WSDOM%');


but what if you dont want exact matching like IN gives and want to do
a LIKE... what can you do.. Here is what I want:


SELECT * FROM language WHERE langkey INLIKE ( SELECT acct_desc || '-%'
FROM dbo.product WHERE acct_code LIKE 'WSDOM%');
perhaps you want "any"

SELECT * FROM language WHERE langkey LIKE ANY ( SELECT acct_desc ||
'-%' FROM dbo.product WHERE acct_code LIKE 'WSDOM%');



--
¡spuɐɥ ou 'ɐꟽ ʞooꞀ

--- news://freenews.netfront.net/ - complaints: news (AT) netfront (DOT) net ---

Reply With Quote
  #7  
Old   
The Quiet Center
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-23-2010 , 01:21 PM



On Aug 18, 1:08*pm, Thomas Kellerer <OTPXDAJCS... (AT) spammotel (DOT) com>
wrote:


Quote:
Although it won't help the OP because he/she
he

Quote:
is using SQL Server (as far as I can tell)
Sybase

Quote:
and last time I checked, SQL Server did not support the row/table constructor using the VALUES clause

Regards
Thomas

Reply With Quote
  #8  
Old   
--CELKO--
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-24-2010 , 06:34 AM



Quote:
Although it won't help the OP because he/she is using SQL Server (as far as I can tell) and last time I checked, SQL Server did not support the row/table constructor using the VALUES clause
SQL Sewrver 2008 does. I have some other tricks with it to convert a
long parameter list into a table. See "VALUES() and Long Parameter
Lists (22 July 2010) at http://www.simple-talk.com/sql/learn...rameter-lists/

Reply With Quote
  #9  
Old   
--CELKO--
 
Posts: n/a

Default Re: I need a LIKE operator with IN functionality - 08-24-2010 , 06:40 AM



That syntax us techncially correct in ANSI/ISO SQL:2003 with a little
work :

SELECT * FROM foobar WHERE langkey LIKE ANY ((SELECT acct_desc ||
'-%' FROM Products WHERE acct_code LIKE 'WSDOM%'));

LANGUAGE is a reserved word and you nee to make the subquery into an
expression.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.