How can I use LIKE
operator with the OR
operator in SQL Server 2005? For example:
SELECT *
FROM table
WHERE column_name LIKE '%boo%' OR '%bar%'
This does not seems to work !
EDIT 1 : RESPONSE TO FIRST ANSWER POSTED:
Cool, that works ! but I just found for the first time that your order conditions do not work with like ... I want to ask is this normal or am I making a mistake? please see below to see what I mean
select *
from <table>
where col_name = 'VAL-QWE'
AND scenario like '%xxx%'
OR scenario like '%yyy%'
if you execute this query, sql server does not cares for col_name = 'VAL-QWE'
condition at all, it just looks at the like condition ?
select *
from table where
column_name like '%boo%'
or column_name like '%bar%'
You need to repeat what the condition is being tested on. In this case, you need to repeat column_name
in your second conditional like
clause.
The like
or any conditional test does not persist from the previous statement/test.
You have totally changed the meaning of your question by your editing, not a good thing to do!
In your modified example, the 'AND' is evaluated before the 'OR' , so your sql is doing the following:
select * from <table> where (col_name = 'VAL-QWE' AND scenario like '%xxx%') or scenario like '%yyy%'
but what I think you want is
select * from <table> where col_name = 'VAL-QWE' AND (scenario like '%xxx%' or scenario like '%yyy%')
简单易用的sql regexp:
select * from <table> where col_name = 'VAL-QWE' AND scenario REGEXP 'xxx|yyy';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.