Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Like statement with multiple values

Status
Not open for further replies.

Malekish

Technical User
Dec 8, 2008
36
US
Hi all, I tried searching for a previous example however using the work 'like' gives many, many results :)

I have a report I've inherited and I'm stuck on a problem. Querying MS SQL Server and I'm trying to get a list of Object_IDs.

Example of table 1

Date_field, object_id, data_field1, data_field2...
20081201, CS4_1_194, 524, 23.....

Example of table 2

Object_ID, Object_Name, Object_Type...
CS4_1_194, 396628@dsm_siemens_hicom, 4

Obviously I can use Object_ID from both tables however the data handed to me includes an extensive list of items, all given with Object_Name instead of Object_ID. What I want to do is find the Object_IDs to build another query.

I know I can
select Object_ID from Table2 where Object_Name like '396628@%'

What is the format if I want to include several of those names? The following error gives me syntax errors.

Select Object_ID from Table2 where Object_Name
like ('396628@%', '396629@%', '396630@%')


Thanks in advance for any help!
 
Select Object_ID from Table2
where Object_Name like '396628@%'
OR Object_Name like '396629@%'
OR Object_Name like '396630@%'


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ok, so I do have to do it as ORs. I found one example on the net after posting this question and was hoping there'd be a better solution.

Thanks much PH!
 
An ANSI SQL another way:
SELECT Object_ID FROM Table2
WHERE SUBSTRING(Object_Name FROM 1 FOR 7) IN ('396628@','396629@','396630@')


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top