<?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>Andy's Blog &#187; How To:</title>
	<atom:link href="http://blog.miloco.com/category/how-to/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.miloco.com</link>
	<description>Why do I have a Blog?  When I find out, I'll tell you.</description>
	<lastBuildDate>Mon, 26 Jul 2010 18:58:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How To: Replace a Partial String in MySQL</title>
		<link>http://blog.miloco.com/2009/01/how-to-replace-a-partial-string-in-mysql.html</link>
		<comments>http://blog.miloco.com/2009/01/how-to-replace-a-partial-string-in-mysql.html#comments</comments>
		<pubDate>Fri, 02 Jan 2009 03:59:28 +0000</pubDate>
		<dc:creator>Andrew Milo</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[How To:]]></category>
		<category><![CDATA[Web Dev]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://blog.miloco.com/?p=667</guid>
		<description><![CDATA[How to replace a partial string in a MySQL database with nothing but SQL, and a real world example of why you might just need to do so one day.]]></description>
			<content:encoded><![CDATA[<p>When developing web sites, sometimes things change from how they were in development to how they need to be in production, especially when it comes to machine names and IP addresses, etc.  If these values are stored in a database, chances are you&#8217;ll need some quick SQL magic to help finalize the migration.</p>
<p>For those of you that just want the info, here you go.  This is a quick and easy way to directly replace one string with another for all rows in a column/field.  <strong>DISCLAIMER: </strong>If you don&#8217;t know what you are doing, you can really screw up your database.  Please take care to do a backup and by all means, if you can&#8217;t fix what you break, don&#8217;t attempt this.</p>
<p><code>UPDATE MyDB.MyTable<br />
SET </code><code>MyDB.MyTable</code><br />
<code> = REPLACE(</code><code>MyDB.MyTable</code><code>.MyField,'OldString','NewString')<br />
WHERE </code><code>MyDB.MyTable.MyField </code><code>like '%OldString%';<br />
</code></p>
<p><span id="more-667"></span></p>
<p><em><strong>Real World Usage:</strong></em></p>
<p>I just ran into this issue when implementing our new <a title="Milowerx Media" href="http://www.milowerx.com" target="_blank">Milowerx Media website</a>.  My brother and I have been setting up the site on a development server that was hosted as a sub domain of the <em>milowerx.com </em>root.  Doing so allowed us to keep up the old site as <em>www.milowerx.com</em> while we created the new site.  When we were finished with the new site, all I needed to do was change the DNS value for the <em>www </em>entry and tell the new server that it was <em>www </em>and not the development subdomain <em>wpdev</em>.</p>
<p>As I&#8217;ve probably mentioned before, I use WordPress to do just about any web related task these days, and the new Milowerx site is no exception.  There are several nice things about using WordPress as a Content Management System.  It is extremely flexible, has a great user community that contributes tons of useful plugins and finally it is extremely stable.</p>
<p>But, this post is about SQL, not WordPress.  I bring up WordPress however because it is a completely dynamic Web Content Publishing platform that is driven entirely by PHP files reading values in a database.  One of the plugins that we used was <a title="WordTube" href="http://wordpress.org/extend/plugins/wordtube/" target="_blank">WordTube</a>, which we chose to help manage the oodles and oodles of media that we have generated over the years.  Its a great plugin, but unfortunately it uses explicit URLs that are stored in the database to generate it playlist values.  When migrating the server from the dev environment to production, all of the references were calling the wrong URL.</p>
<p>Essentially, I needed to find every instance of the development server name <em>wpdev</em> and replace it with <em>www</em> for all of the WordTube tables.  To do so, I ran two MySQL queries that did the trick.</p>
<p><code>UPDATE MyWPDB.wp_wordtube<br />
SET </code><code>MyWPDB</code><code>.wp_wordtube.image<br />
= REPLACE(</code><code>MyWPDB.</code><code>wp_wordtube.image,'http://wpdev.','http://www.')<br />
WHERE </code><code>MyWPDB.</code><code>wp_wordtube.image like '%http://wpdev.%';<br />
</code></p>
<p>and</p>
<p><code>UPDATE MyWPDB.wp_wordtube<br />
SET </code><code>MyWPDB</code><code>.wp_wordtube.file<br />
= REPLACE(</code><code>MyWPDB.</code><code>wp_wordtube.file,'http://wpdev.','http://www.')<br />
WHERE </code><code>MyWPDB.</code><code>wp_wordtube.file like '%http://wpdev.%';<br />
</code></p>
<p>The first query updates the thumbnail images; the second updates the actual movie files.  Presto, change-o, it all works.  <img src='http://blog.miloco.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now, for those of you who know DNS and WordPress, you may be thinking &#8220;Why didn&#8217;t you just mirror <em>wpdev</em> to <em>www</em> so that any call to <em>wpdev </em>would end up pointing in the new, right location?&#8221;  Well, good question.  The answer is that WordTube seems to handle local files one way and remote files another.  Leaving the <em>wpdev</em> reference changed the way thumbnails and movie files were handled in the playlist.  So, technically it would have worked, but the outcome wasn&#8217;t optimal.  And, two easy SQL statements was much easier than slogging through the code of WordTube.  <img src='http://blog.miloco.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.miloco.com/2009/01/how-to-replace-a-partial-string-in-mysql.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

