0

I am trying to select some messages from our customer service queries, WHERE Mmessage owner is ABC, data are LIKE ABCDEF AND message ... AND where the message is either from Customer to CSservice or from CSservice to Customer.

How can I do that?

SELECT Date, From, To, Data 
FROM Ccustomers WITH (NoLock)
WHERE MSGowner = 'ABC' AND Data LIKE '%ABCDEF%' AND 
([From] ='Customer' AND [To] = 'CSservice') OR ([From] ='CSservice' AND [To] = 'Customer') 

ORDER by Date
juergen d
  • 201,996
  • 37
  • 293
  • 362
Petrik
  • 823
  • 2
  • 12
  • 25

2 Answers2

2
SELECT Date, From, To, Data 
FROM Ccustomers WITH (NoLock)
WHERE MSGowner = 'ABC' 
AND Data LIKE '%ABCDEF%' 
AND 
(
   ([From] = 'Customer'  AND [To] = 'CSservice') OR 
   ([From] = 'CSservice' AND [To] = 'Customer')
)
ORDER by Date
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • Thanks a lot. Actually I have figured this out few seconds after posting message :) Still learning SQL :) – Petrik Jan 27 '15 at 11:27
0

Your query was basically correct. But you have to consider that the and connection is "stronger" than the or. To get to your desired output you need to set brackets.

Try this:

SELECT Date, [From], [To], Data 
FROM Ccustomers WITH (NoLock)
WHERE MSGowner = 'ABC' 
AND Data LIKE '%ABCDEF%' 
AND (([From] = 'Customer'  AND [To] = 'CSservice') OR ([From] = 'CSservice' AND [To] = 'Customer'))
ORDER BY Date;
timbmg
  • 3,192
  • 7
  • 34
  • 52