Solved

Select where not in another table

Posted on 2013-01-31
8
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

732 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