Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 In and Like Statement

Author  Topic 

sross81
Posting Yak Master

228 Posts

Posted - 2009-03-23 : 16:49:35
Is it possible to put a Like statement in an In statement? I don't want to hard code like this but they are leaving me no choice.

select * from fdb_medication
where brand_name IN(like('%hand held shower%','raised toilet seat','chucks',
'blood pressure monitor','glucometer','lancets','test strips'))

Thanks in Advance!
Sherri

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-23 : 17:01:38
Use multiple LIKEs with OR.

WHERE brand_name LIKE '%hand held shower%' OR brand_name LIKE '%...%' OR brand_name IN ('blood pressure monitor', ...)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-03-23 : 17:12:09
If there's logic to this, you can always build a query dynamicly as well and use EXEC(...)


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-23 : 17:15:18
Vinnie, how would that help?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

sross81
Posting Yak Master

228 Posts

Posted - 2009-03-23 : 18:23:54
Thanks for your help. I have decided that they are crazy and I am not hard coding this thing its like 36000 names!

quote:
Originally posted by sross81

Is it possible to put a Like statement in an In statement? I don't want to hard code like this but they are leaving me no choice.

select * from fdb_medication
where brand_name IN(like('%hand held shower%','raised toilet seat','chucks',
'blood pressure monitor','glucometer','lancets','test strips'))

Thanks in Advance!
Sherri



Thanks in Advance!
Sherri
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-03-23 : 21:15:37
You can put the brand names that you want to check against in a table and then join on the table. It would SOMETHING LIKE this:
declare @table table (searchTerm varchar(32));
insert into @table (searchTerm) values ('hand held shower');
insert into @table (searchTerm) values ('raised toilet seat');

select
*
from
fb_medication f
inner join @table t on
f.brand_name like '%' + t.searchTerm + '%'

You may want to use a table or temp table instead of a table variable.

If you want to search for exact phrases in some and partial matches in other, include the wild card qualifer (%) in @table.

Also, depending on whether you care about duplicate rows or not, you might want to use select distinct.
Go to Top of Page
   

- Advertisement -