SQL Where clause to ensure 2 columns are not NULL at same time

On the surface, this should be a simple query but I'm not getting the results I expect.
We have 3 columns I'm interested in from a larger table - let's call them Style, City1, and City2
An example data set may look like this:

Style    City1               City2
1          NULL         NULL
2          Boston          NULL
3          NULL          Chicago
4          NULL          NULL
5          New York          Dallas
2          Boston        NULL
2          Boston        Null

I need to write a query that:

Only returns rows where Style is not Null
*SKIPS the Row if BOTH City1 and City2 are Null

That second condition is messing me up.  I wrote the following but get no results:

      SELECT      Style
                  , UPPER(City1) as [City1]
                  , UPPER(City2) as [City2]
                                          
      FROM      ExampleTable

      WHERE      CityType in (1,2,3)
                  AND ((City1 is not null) and (City2 is not null))
                                    
      GROUP BY Style, City1, City2
      ORDER BY Style, City1, City2

This queries a subset of a larger table so there can be many rows where Style, City1 and City2 are the same.  Hence the Group By to reduce it down to distinct values.

CityType is another column in table, but I'm not showing it in the results.  

Here are the results I was expecting to get:

Style    City1             City2
2          Boston          NULL
3          NULL          Chicago
5          New York          Dallas

Instead, I get zero results.  Any thoughts?  Thanks!
dpmoneyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl..
SELECT Style, City1, City2
FROM your_table
WHERE Style IS NOT NULL AND NOT (City1 IS NULL AND City2 IS NULL) 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Copy-paste the below code into your SSMS, execute to verify it meets your requirements, and modify to suit your names
CREATE TABLE #tmp (Style varchar(10), City1 varchar(10), City2 varchar(10)) 

INSERT INTO #tmp (Style, City1, City2) 
VALUES 
(1, NULL, NULL), 
(2, 'Boston', NULL), 
(3, NULL, 'Chicago'), 
(4, NULL, NULL), 
(5, 'New York', 'Dallas'), 
(2, 'Boston', NULL), 
(2, 'Boston', Null)

SELECT Style, City1, City2
FROM #tmp
WHERE Style IS NOT NULL AND NOT (City1 IS NULL AND City2 IS NULL)
GROUP BY Style, City1, City2
ORDER BY Style 

Open in new window

0
dpmoneyAuthor Commented:
Thanks, that is what I was looking to accomplish.  Wasn't sure on the syntax!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Prolly a couple of different ways to pull that off, but that was the first to come to mind.
Thanks for the grade, good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.