David Starkey
David Starkey

Reputation: 1840

Updating table based on zip code table

I am looking for a way to update a table containing city, state, zip, and county by using a different table containing all the necessary information.

To put it simply, I have a table (let's call it Table1) that lists all the cities, states, and counties by zip code (or whatever order you want to put those in). I want to update a different table (call it Table2), specifically city, state, and county, using the zip code from Table1.

EDIT: MS SQL Server

Upvotes: 0

Views: 918

Answers (2)

NG.
NG.

Reputation: 6073

Try this

UPDATE Table2
SET t2.City = t1.City,
t2.state= t1.state,
t2.county= t1.county

FROM Table2 AS t2
LEFT JOIN Table1 t1
ON t2.zipcode= t1.zipcode

Upvotes: 0

NoAlias
NoAlias

Reputation: 9193

UPDATE A
SET A.City = B.City
FROM Table2 AS A
INNER JOIN Table1 B
ON A.PostalCode = B.PostalCode
WHERE A.City IS NULL --Or Some Other Criteria

Upvotes: 4

Related Questions