<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sosuke &#187; mssql</title>
	<atom:link href="http://www.sosuke.com/index.php/tag/mssql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sosuke.com</link>
	<description>Dark music of the gods.</description>
	<lastBuildDate>Wed, 06 Jan 2010 01:34:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Free US Zip Code Database</title>
		<link>http://www.sosuke.com/index.php/2009/05/30/free-us-zip-code-database/</link>
		<comments>http://www.sosuke.com/index.php/2009/05/30/free-us-zip-code-database/#comments</comments>
		<pubDate>Sun, 31 May 2009 01:28:06 +0000</pubDate>
		<dc:creator>sosuke</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[phpmyadmin]]></category>

		<guid isPermaLink="false">http://www.sosuke.com/?p=42</guid>
		<description><![CDATA[So as one of my sites grew I quickly found out that my zip code database was horribly incomplete with only 33234 records and it even had duplicates for some zip codes. I did some searching and came across a post about a zip code csv file but the post goes to what looks like [...]]]></description>
			<content:encoded><![CDATA[<p>So as one of my sites grew I quickly found out that my zip code database was horribly incomplete with only 33234 records and it even had duplicates for some zip codes. I did some searching and came across a post about a <a href="http://coolthingoftheday.blogspot.com/2008/04/free-us-zip-code-database-as-csv.html">zip code csv</a> file but the post goes to what looks like an empty page with no file. A little digging lead me to the <a href="http://mappinghacks.com/data/?C=M;O=A">zip code file</a> download page that gives you a csv with entries like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;78701&quot;,&quot;Austin&quot;,&quot;TX&quot;,&quot;30.27127&quot;,&quot;-97.74103&quot;,&quot;-6&quot;,&quot;1&quot;</pre></div></div>

<p>Awesome! This csv file has 43191 unique records and has entries for each zip code my users have reported as missing. I started by looking up how to import a csv into MSSQL and followed an article called <a href="http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file">Using BULK INSERT to Load a Text File</a> from which I came up with my query.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BULK <span style="color: #993333; font-weight: bold;">INSERT</span> ZipCodes <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'c:<span style="color: #000099; font-weight: bold;">\z</span>ipcodes.csv'</span> <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>FIELDTERMINATOR <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot;&quot;,&quot;&quot;'</span><span style="color: #66cc66;">,</span> ROWTERMINATOR <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>It didn&#8217;t work and after a couple hours of trial and error I threw my hands up in the air and abandoned the bulk insert and fell back on another option of using phpMyAdmin. It sounds crazy but I setup the database in MySQL and imported the csv on the first attempt! Now to get it back to to MSSQL I exported the database as single row imports that looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ZipCodes`</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'78701'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Austin'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'TX'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'30.27127'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-97.74103'</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>A few quick find and replaces to change the database name and remove the ` characters and I was able to run them as queries on my MSSQL database in blocks of 5500 to prevent timeouts.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #66cc66;">&#91;</span>databaseName<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>ZipCodes<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'78701'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Austin'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'TX'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'30.27127'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-97.74103'</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It&#8217;s crucial to have a fairly complete zip code database for distance searching on my <a href="http://diveintothepool.com">free online dating</a> site and I&#8217;m glad I got this sorted out and updated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sosuke.com/index.php/2009/05/30/free-us-zip-code-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
