-1

I have a simple select query on a table, but with different values in LIKE operator. Query is as follows:

SELECT *
FROM BatchServices.dbo.TaskLog
WHERE EntryTime BETWEEN '20190407' AND '20190408' AND
      TaskGroup LIKE '%CSR%' AND
      (LogText LIKE '%error%' OR LogText LIKE '%fail%')

This above query is fine and returning me the expected results but I don't want to have multiple LIKE in a query, so I have already tried something like

SELECT *
from BatchServices.dbo.TaskLog
WHERE taskgroup = 'csr' AND
      LogText IN ( '%error%','%fail%') AND 
      EntryTime>'2019-04-07'
ORDER BY EntryTime ASC

This query is not giving me any results. I am expecting a query which looks smarter than the one I have which returns result. Any help?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
shary.sharath
  • 649
  • 2
  • 14
  • 29

2 Answers2

1

use like operator with OR condition

SELECT * from BatchServices.dbo.TaskLog WHERE taskgroup ='csr' AND 
(LogText like '%error%' or LogText like '%fail%')
AND EntryTime>'2019-04-07' 
ORDER BY EntryTime ASC
Fahmi
  • 37,315
  • 5
  • 22
  • 31
0

The LIKE operators are not the problem. It's the leading wild cards. Unless you cant get rid of those, your optimization options are going to be limited to making sure you have a covering index on EntryTime... That and replacing the "*" with the specific columns you need.

Jason A. Long
  • 4,382
  • 1
  • 12
  • 17