August 4, 2011 at 4:41 am
Hi Pals,
Not sure wwhatswrong with this query but whenever I am running this, I am getting all those rows with State='closed' which it should ignoring.
SELECT [ID],Priority,Title,[Created By],[Assigned To], [State],History,[CReated Date],[Closed Date], [Closed Date], Comments, Keywords
FROM dbo.t_BugReport WHERE State <> N'Closed'
AND Title LIKE N'%Bahamas%'
OR History LIKE N'%TheGreatCharlie%'
OR [Repro Steps] LIKE N'%Anything%'
not sure why it returning even those rows which ha state=closed..
August 4, 2011 at 4:55 am
Vishal Singh (8/4/2011)
Hi Pals,Not sure wwhatswrong with this query but whenever I am running this, I am getting all those rows with State='closed' which it should ignoring.
SELECT [ID],Priority,Title,[Created By],[Assigned To], [State],History,[CReated Date],[Closed Date], [Closed Date], Comments, Keywords
FROM dbo.t_BugReport WHERE State <> N'Closed'
AND Title LIKE N'%Bahamas%'
OR History LIKE N'%TheGreatCharlie%'
OR [Repro Steps] LIKE N'%Anything%'
not sure why it returning even those rows which ha state=closed..
Check the logic of your WHERE clause 😉
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 4, 2011 at 5:04 am
Tried placing the State <> 'Closed' filter here and there but not helping much.
If I am removing one 'OR' filter its working but I need to make it work with all the filters mentioned in the query.
August 4, 2011 at 5:08 am
Try these two variants of your WHERE clause:
WHERE ([State] <> N'Closed'
AND Title LIKE N'%Bahamas%')
OR History LIKE N'%TheGreatCharlie%'
OR [Repro Steps] LIKE N'%Anything%'
WHERE [State] <> N'Closed'
AND (Title LIKE N'%Bahamas%'
OR History LIKE N'%TheGreatCharlie%'
OR [Repro Steps] LIKE N'%Anything%')
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 4, 2011 at 5:10 am
Vishal Singh (8/4/2011)
Hi Pals,Not sure wwhatswrong with this query but whenever I am running this, I am getting all those rows with State='closed' which it should ignoring.
SELECT [ID],Priority,Title,[Created By],[Assigned To], [State],History,[CReated Date],[Closed Date], [Closed Date], Comments, Keywords
FROM dbo.t_BugReport WHERE State <> N'Closed'
AND Title LIKE N'%Bahamas%'
OR History LIKE N'%TheGreatCharlie%'
OR [Repro Steps] LIKE N'%Anything%'
not sure why it returning even those rows which ha state=closed..
Brackets are your friend in this instance, look at the where clause and consider which part needs to be put into the brackets.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
August 4, 2011 at 5:11 am
http://msdn.microsoft.com/en-us/library/ms190276.aspx
http://msdn.microsoft.com/en-us/library/ms186992.aspx
the above two article explain how operators work. You need to group operators using brackets to make sure you get the required results.
e.g
GO
SELECT ProductID, ProductModelID
FROM AdventureWorks2008R2.Production.Product
WHERE ProductModelID = 20 OR ProductModelID = 21
AND Color = 'Red'
AND
SELECT ProductID, ProductModelID
FROM AdventureWorks2008R2.Production.Product
WHERE (ProductModelID = 20 OR ProductModelID = 21)
AND Color = 'Red'
Are two different queries, because in the first case the where condition is understood as follows:-
(Find all Red cars with modelid = 21 ) or All cars with model 20
the second where clause is understood as
Find all red cars where the model ID is (either 20 or 21)
August 4, 2011 at 5:34 am
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply