Thomas Owens
Thomas Owens

Reputation: 116187

How do you OR two LIKE statements?

I have tried the following two statements:

I figured one of the two statements would work, but they aren't.

Upvotes: 56

Views: 187263

Answers (8)

Elie
Elie

Reputation: 13855

try

SELECT col FROM db.tbl WHERE (col LIKE 'str1' or col LIKE 'str2') AND col2 = num

Upvotes: 2

Tom Ritter
Tom Ritter

Reputation: 101380

SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = num

Upvotes: 100

Dominic Flynn
Dominic Flynn

Reputation: 71

I assume what you want is,

SELECT col FROM db.tbl WHERE col LIKE '%str[12]%' AND col2 = num

That will literally find all cases where (col like %str1% or col like %str2%) AND col2 = num

Upvotes: 3

AMF
AMF

Reputation: 7

In recent versions of SQL you can use the IN operator:

SELECT col FROM db.tbl WHERE col IN('str1','str2') AND col2 = num

Upvotes: 0

Mohammad Fareed
Mohammad Fareed

Reputation: 1972

USE % at the Start and End of the String. So that it will check the Sub-Strings as well

SELECT col FROM db.tbl WHERE (col LIKE '%str1%' OR col LIKE '%str2%') AND col2 = num

Upvotes: 6

Eclipse
Eclipse

Reputation: 45533

The "OR" operator applies to expressions. "LIKE 'str1'" and "LIKE 'str2'" are not valid expressions, since the "LIKE" operator requires both a left hand side and a right hand side.

page LIKE ('str1' OR 'str2')

would first try to OR together 'str1' and 'str2', giving the value TRUE. It would then try and evaluate page LIKE TRUE, and report an error.

Upvotes: 3

warren
warren

Reputation: 33453

I believe you need WHERE ((page LIKE 'str1') OR (page LIKE 'str2'))

Upvotes: 5

Daniel Spiewak
Daniel Spiewak

Reputation: 55123

Think simple:

SELECT col FROM db.tbl WHERE (col LIKE 'str1' OR col LIKE 'str2') AND col2 = ...

Upvotes: 2

Related Questions