<?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>Vance Lucas &#187; MySQL</title>
	<atom:link href="http://www.vancelucas.com/blog/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vancelucas.com</link>
	<description>Web Entrepreneur and PHP5 Guru</description>
	<lastBuildDate>Wed, 07 Jul 2010 17:37:10 +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>MySQL Series: How to Detect UTF-8 and Multi-byte Characters</title>
		<link>http://www.vancelucas.com/blog/mysql-series-how-to-detect-utf-8-and-multi-byte-characters/</link>
		<comments>http://www.vancelucas.com/blog/mysql-series-how-to-detect-utf-8-and-multi-byte-characters/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 15:44:39 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=542</guid>
		<description><![CDATA[Multi-byte characters can cause quite a few headaches for the unsuspecting webmaster. Sometimes all you need to do to figure out how to fix the problem is detect which database records have UTF-8 data in them and which ones do not. If you&#8217;ve been scanning records manually, stop now. Here&#8217;s a quick query to cure [...]]]></description>
			<content:encoded><![CDATA[<p>Multi-byte characters can cause quite a few headaches for the unsuspecting webmaster. Sometimes all you need to do to figure out how to fix the problem is detect which database records have UTF-8 data in them and which ones do not. If you&#8217;ve been scanning records manually, stop now. Here&#8217;s a quick query to cure your ales:</p>
<p>Return all the rows with multi-byte characters in table <em>posts</em> on field <em>title</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span>
<span style="color: #993333; font-weight: bold;">FROM</span> posts
<span style="color: #993333; font-weight: bold;">WHERE</span> LENGTH<span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;&gt;</span> CHAR_LENGTH<span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This does pretty much what it looks like: returns all the rows in the posts table where the title&#8217;s character length does not match the title&#8217;s length. This works because the LENGTH function returns the <em>number of bytes</em> in the string, while the CHAR_LENGTH function returns the <em>number of characters</em> in the string. If the string contains multi-byte characters (individual characters that are made up of more than one byte) like international UTF-8 characters, the two functions will return different numbers and will not be equal, thus including that row in your results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mysql-series-how-to-detect-utf-8-and-multi-byte-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Series: Return NULL Values First With Descending Order</title>
		<link>http://www.vancelucas.com/blog/mysql-series-return-null-values-first-with-descending-order/</link>
		<comments>http://www.vancelucas.com/blog/mysql-series-return-null-values-first-with-descending-order/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 21:28:55 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=486</guid>
		<description><![CDATA[Sometimes there are unique situations where you need to order query results by a particular field in descending order, but also need NULL values first. The default (and logical) behavior of MySQL in this case is to return NULL values last, because in descending order they have the lowest value (none). But what if you [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes there are unique situations where you need to order query results by a particular field in descending order, but also need NULL values first. The default (and logical) behavior of MySQL in this case is to return NULL values last, because in descending order they have the lowest value (none). But what if you really need to reverse this and force NULL values to the top of the result set?<br />
<span id="more-486"></span><br />
I recently ran into this situation with movie results. The business requirement was that the movies be displayed in descending order by release date, so that the newest ones appeared first. Okay, pretty simple &#8211; until I looked at the results. Turns out, we had data on movies that were currently in production, but did not yet have a release date set. I accounted for this earlier by allowing the release date column to be NULL in the table schema, but forgot about the ramifications of that on my queries. These movies were obviously newer, but were always displayed last on the results page because of their NULL release dates. Sounds like the perfect scenario for a conditional statement.</p>
<p>Using the <a href="http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html">MySQL Control Flow Functions</a>, I quickly crafted an IF() statement in the SELECT portion of the query that looked like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #009900;">IF</span><span style="color: #FF00FF;">&#40;</span>m.date_released <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #CC0099; font-weight: bold;">OR</span> m.date_released <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0000-00-00'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> in_production</pre></td></tr></table></div>

<p>So if the release date has a NULL value or has been inserted as a blank string and formatted to &#8216;0000-00-00&#8242;, it is given a value of 1, and 0 otherwise. We then sort descending by our new &#8216;in_production&#8217; alias in the ORDER BY clause to get the NULL or empty values on top (assigned a 1 value), and then by release date and other criteria second.</p>
<p>The whole query looks something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> m.<span style="color: #CC0099;">*</span><span style="color: #000033;">,</span> <span style="color: #009900;">IF</span><span style="color: #FF00FF;">&#40;</span>m.date_released <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #CC0099; font-weight: bold;">OR</span> m.date_released <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0000-00-00'</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> in_production
<span style="color: #990099; font-weight: bold;">FROM</span> movies <span style="color: #990099; font-weight: bold;">AS</span> m
<span style="color: #990099; font-weight: bold;">ORDER BY</span> in_production <span style="color: #990099; font-weight: bold;">DESC</span><span style="color: #000033;">,</span> m.date_released <span style="color: #990099; font-weight: bold;">DESC</span></pre></td></tr></table></div>

<p>It&#8217;s an easy way to manipulate the result set in MySQL without slowing down the query or issuing multiple queries.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mysql-series-return-null-values-first-with-descending-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
