Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL ignoring multiple AND clauses


TSQL ignoring multiple AND clauses

Author
Message
kate osborn
kate osborn
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 15
I am running a query that where an input parameter is set to "N" it should ignore rows where column a is "x" and column b is "-99"

The query I am trying is:

DECLARE @IncludeUnavailableBookings VARCHAR(1)
SET @IncludeUnavailableBookings = 'N'

SELECT
*
FROM
Table1 A
LEFT JOIN Table2 B ON B.ID = A.ID
WHERE
(
(@IncludeUnavailableBookings = 'Y') OR
(@IncludeUnavailableBookings = 'N' AND (A.Col1 <> 'X' AND A.Col2<> '-99'))
)

However this excludes all rows where EITHER Col1 = 'X' OR A.Col2= '-99'
What I want to do is only exclude rows where both conditions are met. I can get around this by concatenating the two columns as follows:

WHERE
(
(@IncludeUnavailableBookings = 'Y') OR
(@IncludeUnavailableBookings = 'N' AND A.Col1+CONVERT(VARCHAR(20),A.Col2) <> 'X-99'))


However can anyone tell me why the first query is not working the same way please?
Sushil Dwivedi
Sushil Dwivedi
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 142
if you put OR in place of AND (Col1 <> 'X' or Col2<> '-99'), you will get same result from both the Sql
create table #temp (id int,col1 varchar(30),col2 varchar(30))
insert into #temp(id,col1,col2)
select 1,'A','-99'
union all
select 2,'B','10'
union all
select 3,'X','-99'
union all
select 4,'Z','-99'
union all
select 5,'X','-99'

DECLARE @IncludeUnavailableBookings VARCHAR(1)
SET @IncludeUnavailableBookings = 'N'
select * from #temp where @IncludeUnavailableBookings = 'N' AND (Col1 <> 'X' or Col2<> '-99')
select * from #temp where @IncludeUnavailableBookings = 'N' AND (Col1+CONVERT(VARCHAR(20),Col2) <> 'X-99')
anastke
anastke
SSC-Enthusiastic
SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)

Group: General Forum Members
Points: 182 Visits: 2354
negative logic: ( a & b) <=> (!a | !b)

you want to select

WHERE a OR (b AND NOT (c AND d))

which would be equiv. to

WHERE a OR (b AND (NOT c OR NOT d))

so either:

(@IncludeUnavailableBookings = 'Y') OR
(@IncludeUnavailableBookings = 'N' AND NOT (A.Col1 = 'X' AND A.Col2 = '-99'))

or:

(@IncludeUnavailableBookings = 'Y') OR
(@IncludeUnavailableBookings = 'N' AND (A.Col1 <> 'X' OR A.Col2 <> '-99'))
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search