How to use "and" and "or" in a "Where" clause How to use "and" and "or" in a "Where" clause sql sql

How to use "and" and "or" in a "Where" clause


You need to wrap your Paid_Out_Amoutn and Paid_Out_Comment criteria in a second set of parentheses:

SELECT Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime,     Update_UserName, Till_Number FROM Paid_Out_Tb WHERE (Store_Id = 1929) AND (Paid_Out_Datetime >=     DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND     (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) AND     (        (Paid_Out_Amount > 50) OR (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')    )


It really depends on what you are using the OR for. You have Paid_Out_Amount is greater than 50 or Paid_Out_Amount is like N%Filter%. Add some brackets to you clause

WHERE (    (1 = 1)    AND    (2 = 2))OR( 3 = 3 )


It looks like you are missing one set of brackets:

SELECT            Store_Id    , Paid_Out_Amount    , Paid_Out_Comment    , Paid_Out_Datetime    , Update_UserName    , Till_NumberFROM                Paid_Out_TbWHERE            Store_Id = 1929    AND Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)    AND Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)    AND    (        Paid_Out_Amount > 50        OR        LOWER(Paid_Out_Comment) LIKE '%filter%'    )

You also need to look at that LIKE command - updated the code to set the comments all to lowercase and search for the word filter.

Also check out the command BETWEEN for your Paid_Out_Datetime check