| Author |
Topic |
|
itmasterw
Yak Posting Veteran
90 Posts |
Posted - 2010-05-05 : 12:54:32
|
Hi,In the interest of making this short I will show a subset here of what I have.I have one table of zip codesIn another table I have various fields with one of them being sip codes. Now some time they get entered wrong so I want to pull them out. So in table A, I have Zips146281234412365123741236912623In table B, I have Address city zipasd Buffalo ghe Rochester 12344kjh Troy 12A65ghkj Yonkers 12374uhe Bronx 1236u9hjk Elmwood 12623So my query should pull out Address city zipasd Buffalo NULLkjh Troy 12A65uhe Bronx 1236u9I tried writing this query different ways and I come close but I cannot seem to get this. Does anyone have any ideas?Thank you ITM |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-05-05 : 13:23:20
|
This?SELECT *FROM table_BWHERE zip IN (SELECT zips FROM table_A); |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-05 : 13:26:51
|
| Try this:Declare @ZipTab table(ZipCode varchar(10))Insert into @ZipTabSelect 14628 union Select 12344 union Select 12365 union Select 12374 union Select 12369 union Select 12623 Declare @TableB table(Address varchar(50),city varchar(50),zip varchar(10))Insert into @TableBSelect 'asd', 'Buffalo', null union Select 'ghe', 'Rochester','12344' unionSelect 'kjh', 'Troy','12A65' unionSelect 'ghkj', 'Yonkers','12374' unionSelect 'uhe', 'Bronx' ,'1236u9' unionSelect 'hjk', 'Elmwood' ,'12623' Select B.* from @TableB B where not exists (Select 1 from @ZipTab z where z.ZipCode = B.zip)Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-05 : 14:43:05
|
quote: Originally posted by ms65g This?SELECT *FROM table_BWHERE zip IN (SELECT zips FROM table_A);
shouldnt it be NOT IN?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-05-05 : 15:41:20
|
| NOT IN instead IN? maybe. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-05 : 18:25:50
|
Join method:SELECT B.*FROM @TableB AS BLEFT OUTER JOIN @ZipTab AS Zip ON B.zip = Zip.ZipCodeWHERE Zip.ZipCode IS NULL |
 |
|
|
itmasterw
Yak Posting Veteran
90 Posts |
Posted - 2010-05-05 : 18:40:40
|
Thank you quote: Originally posted by ms65g This?SELECT *FROM table_BWHERE zip IN (SELECT zips FROM table_A);
ITM |
 |
|
|
itmasterw
Yak Posting Veteran
90 Posts |
Posted - 2010-05-05 : 18:42:45
|
Thanks everyone I really appreciate your helpquote: Originally posted by itmasterw Thank you quote: Originally posted by ms65g This?SELECT *FROM table_BWHERE zip IN (SELECT zips FROM table_A);
ITM
ITM |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-05-05 : 19:23:22
|
You are welcome.quote: Originally posted by itmasterw Thank you quote: Originally posted by ms65g This?SELECT *FROM table_BWHERE zip IN (SELECT zips FROM table_A);
ITM
|
 |
|
|
|