Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Eliminating WHERE clauses with NOT
Message
From
05/11/2014 01:58:50
 
 
To
04/11/2014 11:24:14
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01610458
Message ID:
01610500
Views:
55
>>It's boolean algebra
>>
>>
>>NOT ( A and B) 
>>
>>is
>>
>>NOT(A) or NOT(B)
>>
>>
>>
>>So
>>
>>NOT (Client.NoStatus=@NoStatus AND Client.ModDate<@ModDate)
>>
>>NOT( Client.NoStatus=@NoStatus ) OR NOT(Client.ModDate<@ModDate)
>>
>>is
>>(Client.NoStatus <> @NoStatus) OR ( Client.ModDate>= @ModDate )
>>
>>
>>
>>Also
>>
>>
>>NOT ( A or B)
>>is
>>NOT(A) and NOT(B)
>>
>
>Thanks, this provides valuable information.
>
>If you look at Message #1610467, you will see a more realistic command I have to execute.
>
>Based on your reply, from that referred message, does this mean the second command, the one without the NOT clauses, wouldn't need any parentheses?


I think the second statement needs parentheses. Without, the ANDs will be executed before the ORs

http://msdn.microsoft.com/en-us/library/ms190276.aspx

Your statement
SELECT Client.Numero FROM Client (NOLOCK)
 WHERE (Client.ModDate>=@ModDate OR Client.NoStatus<>@NoStatus) AND
  (Client.ModDate>=@ModDate2 OR Client.NoStatus<>@NoStatus2) AND
  (Client.ModDate>=@ModDate3 OR Client.NoStatus<>@NoStatus3)
without parentheses would be
SELECT Client.Numero FROM Client (NOLOCK)
 WHERE Client.ModDate>=@ModDate OR Client.NoStatus<>@NoStatus AND
  Client.ModDate>=@ModDate2 OR Client.NoStatus<>@NoStatus2 AND
  Client.ModDate>=@ModDate3 OR Client.NoStatus<>@NoStatus3
and is the same as
SELECT Client.Numero FROM Client (NOLOCK)
 WHERE Client.ModDate>=@ModDate 
      OR ( Client.NoStatus<>@NoStatus AND  Client.ModDate>=@ModDate2)
     OR (Client.NoStatus<>@NoStatus2 AND  Client.ModDate>=@ModDate3 )
    OR (Client.NoStatus<>@NoStatus3)
which is different
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform