Solved

Select where not in another table

Posted on 2013-01-31
8
279 Views
Last Modified: 2013-02-03
I have 2 tables  
dbo.WideCityList  and dbo.PostalCodes.
I want to find all widecitylist.City, WideCityList.State that does not exist in postalCodes.Cityname, PostalCodes.ProvinceAbbr


All cities in WideCityList have a dash "-" where there should be a space.  
So in WideCity list it is "Los-Angeles" and in PostalCodes it is "Los Angeles"

So a city+state in WideCityList that does not exist in PostalCodes will need to have a replace function for any dashes into spaces.  Hope that makes sense!
0
Comment
Question by:arthurh88
8 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 38839079
Try this:

SELECT w.City, w.State 
FROM WideCityList w LEFT JOIN postalCodes p
ON w.city  = p.Cityname AND w.State = p.ProvinceAbbr
WHERE p.Cityname IS NULL AND p.ProvinceAbbr IS NULL

Open in new window

0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 250 total points
ID: 38839087
Two ways:
SELECT REPLACE(City,'-',' ') City,State
FROM WideCityList t
WHERE NOT EXISTS (SELECT NULL FROM PostalCodes WHERE REPLACE(City,'-','')=Cityname AND State=ProvinceAbbr)

OR

SELECT REPLACE(City,'-',' ') City,State
FROM WideCityList t
LEFT JOIN PostalCodes
ON REPLACE(City,'-','')=Cityname AND State=ProvinceAbbr
WHERE Cityname IS NULL
0
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38839255
If you provide with some sample data and desired result ... it will help
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:arthurh88
ID: 38840437
that worked great, thank you
0
 

Author Comment

by:arthurh88
ID: 38840781
oops, meant to also give credit to you Cluskitt, because you had the replace function i needed.
0
 

Expert Comment

by:zombie2020
ID: 38842492
Another solution:

select replace(widecitylist.City, '-',' ') ,
            replace(WideCityList.State, '-','')
from dbo.WideCityList
except
select postalCodes.Cityname,
            PostalCodes.ProvinceAbbr
from dbo.PostalCodes;
0
 

Author Closing Comment

by:arthurh88
ID: 38849098
it was all very helpful to me, i split credit.  thank you and my apologies
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question