Alex Rivera | Logout

How to write an SQL statement to use "LIKE" for nine different values?

Asked 2010-10-14T17:40:56.163
11

I have to select all the records where:

where FIELD2 like '%value21%'
or FIELD2 like '%value22%'
or FIELD2 like '%value23%'
-- repeat up to
or FIELD2 like '%value29%'

Here, value21, ..., value29 are parameters which the user can put in a form before submitting the query. They are (not subsequent) numerical codes, and FIELD2 is a database column holding a string value.

Which is the most compact form to write down my SQL query?

Note: This is related to an earlier question, but this needs LIKE rather than equals.

Edit
Report

1 Answer

1

One way:

select Field1, Field2 from Table where Field2 like '%val%'
UNION
select Field1, Field2 from Table where Field2 like '%val2%'
UNION
select Field1, Field2 from Table where Field2 like '%val2%'

etc.

answered 2010-10-14T19:03:03.980

Your Answer