Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase ««12

State County City Problem Expand / Collapse
Posted Tuesday, July 9, 2013 7:58 AM

SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, October 28, 2016 10:59 AM
Points: 29, Visits: 85
Greetings. I would recommend you investigate how the US Census Bureau handles geography. In fact, they already have a comprehensive coding for State/County/Place for the entire US, including areas not covered by all levels of geography - like Native American land, islands, etc. A good place to start is their Guide to State and Local Geography:

You can find links to hierarchical diagrams, reference materials and other useful documents. You may find the article on ANSI codes of use:

There is even a download page if you want to import there geographical codes:

Working with a major municipal government which happens to fall within five separate counties, we run into these issues a lot.

I would also caution you about using ZIP codes, as they are not really geographic areas but rather collections of postal carrier routes. In fact, the Census Bureau disclaims this on their page dealing with postal codes. US Postal Service builds their ZIP routes solely on business needs and cross city, county, even state lines with a single ZIP code.

The DBA mantra should always be "know your data, know your data" so understanding these relationships is important before you make business decisions based upon them. ZIP codes were meant to facilitate mail delivery - that is all. For other types of business, particularly Public Safety (which we support) you need something different.

I hope that this is useful to you.

Good luck.

Post #1471635
Posted Tuesday, July 9, 2013 8:02 AM



Group: General Forum Members
Last Login: Yesterday @ 2:44 PM
Points: 16,134, Visits: 16,835
Please note: this thread started in 2008.


Need help? Help us help you.

Read the article at for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1471637
Posted Wednesday, July 10, 2013 8:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 1, 2015 1:52 PM
Points: 2, Visits: 22

The census web site has raw data available for download. The data for each state will have one master "Geo" file for geography info with a primary key called LOGRECNO and related data files with a forign key of the same name. Look for a column called Summary level or Sumlev. The Summary level for Counties is identified as 050, places (cities) is 160 etc. The County and place columns wil give you the County or Place or identifer, sometimes referred to as the FIPS Code. The "Name" column wil give you it's name. You can query the geo file to get your information.

see the document below. Use the summary levels and other Geo identifying fields\columns will filter data accordingly. It is important to understand the table structures they use.

Maybe this will help.
Post #1472164
Posted Friday, June 26, 2015 5:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 27, 2016 9:52 AM
Points: 4, Visits: 63
Great points in extending the data by everyone.

If we're working with just the tables in the question, you could replace the StateID column in the City table with a CountyID column so that the structure is:

State2 table:

StateID (Identity, primary key),
Name (full state name)
TwoLetterCode ('AL')

CountyID (Identity, primary key),
Name (full county name),
StateID (from the State2 table)

CityID (Identity, primary key)
Name (full city name),
CountyID (from the County2 table, primary key)

Foreign keys:
County2.StateID to State2.StateID
County2.CountyID to City.CountyID
Post #1698039
Posted Friday, June 26, 2015 6:13 AM



Group: General Forum Members
Last Login: Today @ 5:18 AM
Points: 45,619, Visits: 44,147
Please note: 7 year old thread.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1698049
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse