Search code examples
sqlsql-serversql-likesql-in

How can query be optimized?


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?


Solution

  • 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