x

OR statement in WHERE clause leaving out information in query

I have an OR statement in my WHERE clause, but it keeps leaving out rows of information. I need this query to return all of the data to me, not leave out rows. There are accounts that may have a 900000 series account number, but I don't want those accounts. I want this query to return to me any account that we have under 900000, and if there is no account, I still want the rows back so that I can manually look for an account or create a new account for the address. Can anyone help? The WHERE clause is below:

WHERE (((dbo_SAP_CUST.KUNNR)<'0000900000' 
AND dbo_SAP_CUST.Code = 'Z8'
OR (dbo_SAP_CUST.KUNNR) Is Null))

ORDER BY CASEDATA.ID;
more ▼

asked Oct 28, 2011 at 08:35 AM in Default

faith gravatar image

faith
1 1 1 1

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

You might try grouping your WHERE conditions using parentheses. The way it is currently written, it's wide open for interpretation. It could be WHERE KUNNR < 9000 AND (Code='Z8' OR KUNNR IS NULL) which would ignore the NULL KURRN column in the OR because it didn't qualify for the KUNNR < '0000900000' portion.

Here are a couple of WHERE options that will allow NULL KUNNR values into the results.

-- every row with qualifying code that has a qualifying or null KUNNR
WHERE (KUNNR < '0000900000' OR KUNNR IS NULL)
AND Code = 'Z8'

-- any row with qualifying KUNNR and Code PLUS every row with null KUNNR
WHERE (KUNNR < '0000900000' AND Code = 'Z8')
OR KUNNR IS NULL
more ▼

answered Oct 28, 2011 at 10:57 AM

KenJ gravatar image

KenJ
19.4k 1 3 11

+1 I agree with @KenJ. The way @faith has parenthesised the WHERE clause is, to me, confusing.
Oct 28, 2011 at 11:58 AM ThomasRushton ♦
(comments are locked)
10|1200 characters needed characters left

I tried the 2 you gave me. Neither gave me the rows back that didn't have an account number. Here is the SQL in full. I don't know if it will help figure this out, but I'm hoping it does! KUNNR is SAP's name for account number, if you need that info as well. I really need to take a class in this.

SELECT DISTINCT 
dbo_SAP_CUST.KUNNR AS AcctNum, 
CASEDATA.ID, 
CASEDATA.*, 
dbo_SAP_CUST.NAME1 AS SAPName, 
dbo_SAP_CUST.STRAS AS SAPOrigAddress, 
dbo_SAP_CUST.STRAS, 
dbo_SAP_CUST.ZCode, 
dbo_SAP_CUST.CGN, 
dbo_SAP_CUST.VIP, 
dbo_SAP_CUST.Name1_1 AS SalesRep, 
dbo_SAP_CUST.Code AS RepType, 
dbo_SAP_CUST.KDGRP AS Segment, 
dbo_SAP_CUST.VKORG AS CompanyCode
FROM CASEDATA 
LEFT JOIN dbo_SAP_CUST ON 
(LEFT(CASEDATA.Zip,5)=LEFT(dbo_SAP_CUST.Zip,5)) 
     AND (CASEDATA.State = dbo_SAP_CUST.State) 
     AND (LEFT(CASEDATA.NewAddy,6)=LEFT(dbo_SAP_CUST.STRAS,6))
WHERE (((dbo_SAP_CUST.KUNNR)<'0000900000' 
AND dbo_SAP_CUST.Code = 'Z8'
OR (dbo_SAP_CUST.KUNNR) Is Null))
ORDER BY CASEDATA.ID
more ▼

answered Oct 28, 2011 at 12:42 PM

faith gravatar image

faith
1 1 1 1

(comments are locked)
10|1200 characters needed characters left
SELECT DISTINCT 
dbo_SAP_CUST.KUNNR AS AcctNum, 
CASEDATA.ID, 
CASEDATA.*, 
dbo_SAP_CUST.NAME1 AS SAPName, 
dbo_SAP_CUST.STRAS AS SAPOrigAddress, 
dbo_SAP_CUST.STRAS, 
dbo_SAP_CUST.ZCode, 
dbo_SAP_CUST.CGN, 
dbo_SAP_CUST.VIP, 
dbo_SAP_CUST.Name1_1 AS SalesRep, 
dbo_SAP_CUST.Code AS RepType, 
dbo_SAP_CUST.KDGRP AS Segment, 
dbo_SAP_CUST.VKORG AS CompanyCode
FROM CASEDATA 
LEFT JOIN dbo_SAP_CUST ON 
(LEFT(CASEDATA.Zip,5)=LEFT(dbo_SAP_CUST.Zip,5)) 
     AND (CASEDATA.State = dbo_SAP_CUST.State) 
     AND (LEFT(CASEDATA.NewAddy,6)=LEFT(dbo_SAP_CUST.STRAS,6))
WHERE (dbo_SAP_CUST.KUNNR<'0000900000' OR dbo_SAP_CUST.KUNNR Is Null)
AND dbo_SAP_CUST.Code = 'Z8'
ORDER BY CASEDATA.ID
Would that give you results?
more ▼

answered Oct 28, 2011 at 02:52 PM

Magnus Ahlkvist gravatar image

Magnus Ahlkvist
16.5k 16 19 33

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x12

asked: Oct 28, 2011 at 08:35 AM

Seen: 1549 times

Last Updated: Oct 28, 2011 at 10:36 AM