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

avatar 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

avatar image

KenJ
24.5k 3 10 19

  • 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

avatar 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

avatar image

Magnus Ahlkvist
21.1k 19 39 42

(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.

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:

x17

asked: Oct 28, 2011 at 08:35 AM

Seen: 2641 times

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

Copyright 2016 Redgate Software. Privacy Policy