<?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>youdidwhatwithtsql.com &#187; order by column value</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/order-by-column-value/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ordering by Column Value in SQL Server &amp; MySQL</title>
		<link>http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384</link>
		<comments>http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384#comments</comments>
		<pubDate>Sun, 27 Sep 2009 15:40:19 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[ORDER BY]]></category>
		<category><![CDATA[order by column value]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384</guid>
		<description><![CDATA[Today I needed to order some data by specific column value and I recalled the really handy FIELD function in MySQL. Here’s a demo of this feature in MySQL ?View Code MYSQL# Test table CREATE TABLE City &#40; Id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, City VARCHAR&#40;50&#41; &#41;; &#160; # Insert some test data INSERT [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384">Ordering by Column Value in SQL Server &amp; MySQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today I needed to order some data by specific column value and I recalled the really handy <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field" target="_blank">FIELD</a> function in <a href="http://www.mysql.com" target="_blank">MySQL</a>. Here’s a demo of this feature in <a href="http://www.mysql.com" target="_blank">MySQL</a></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p384code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3845"><td class="code" id="p384code5"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Test table</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> City
<span style="color: #FF00FF;">&#40;</span>
	Id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	City <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Insert some test data</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> City
<span style="color: #FF00FF;">&#40;</span>
	City
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'London'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Hong Kong'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'New York'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Bangkok'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Los Angeles'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Paris'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Singapore'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Moscow'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Lagos'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008000;">'Johannesburg'</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Using the <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field" target="_blank">FIELD</a> function we can order the resultset how ever we like.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p384code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3846"><td class="code" id="p384code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Ordering by the value of City</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> City
<span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">FIELD</span><span style="color: #FF00FF;">&#40;</span>City<span style="color: #000033;">,</span> <span style="color: #008000;">'Moscow'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Lagos'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Hong Kong'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'New York'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Johannesburg'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Singapore'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'London'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Paris'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Bangkok'</span><span style="color: #000033;">,</span> 
		     <span style="color: #008000;">'Los Angeles'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/mysql_ordering_by_column_value.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mysql_ordering_by_column_value" border="0" alt="mysql ordering by column value thumb Ordering by Column Value in SQL Server &amp; MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/mysql_ordering_by_column_value_thumb.png" width="190" height="244" /></a> </p>
<p>This is all very well but I needed to do this using <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>. I immediately though of using a <a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blank">CASE</a> statement to do the ordering but thought I’d try to find something as easy as the <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field" target="_blank">FIELD</a> function. This search proved to be unfruitful with the <a href="http://msdn.microsoft.com/en-us/library/ms188723.aspx" target="_blank">documentation</a> pointing me nowhere. Should anybody know of a simpler way then please let me know. In the end I settled for a ordering using a CASE statement. The script below contains 2008 <a href="http://msdn.microsoft.com/en-us/library/bb510741.aspx" target="_blank">TSQL</a>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p384code7'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3847"><td class="code" id="p384code7"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Test table</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> City
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span>,
	City <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Insert some test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> City
<span style="color: #808080;">&#40;</span>
	City
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'London'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Hong Kong'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'New York'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Bangkok'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Los Angeles'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Paris'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Singapore'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Moscow'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Lagos'</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Johannesburg'</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now lets order our data by the value of the <strong>City</strong> column.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p384code8'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3848"><td class="code" id="p384code8"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Ordering by the value of City</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> City
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #0000FF;">CASE</span><span style="color: #808080;">&#40;</span>City<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Moscow'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Lagos'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">2</span> 
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Hong Kong'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">3</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'New York'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">4</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Johannesburg'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">5</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Singapore'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">6</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'London'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">7</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Paris'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">8</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Bangkok'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">9</span>
		<span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Los Angeles'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">10</span>
	 <span style="color: #0000FF;">END</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/sql_server_ordering_by_column_value.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql_server_ordering_by_column_value" border="0" alt="sql server ordering by column value thumb Ordering by Column Value in SQL Server &amp; MySQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/sql_server_ordering_by_column_value_thumb.png" width="192" height="239" /></a></p>
<p>Both methods are perfectly functional but <a href="http://www.mysql.com" target="_blank">MySQL</a> wins for me, on the grounds on simplicity, and a few less keystrokes!</p>
<p><map name='google_ad_map_384_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/384?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_384_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=384&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fordering-by-column-value-in-sql-server-mysql%2F384' title="Ordering by Column Value in SQL Server &amp; MySQL" alt=" Ordering by Column Value in SQL Server &amp; MySQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384">Ordering by Column Value in SQL Server &amp; MySQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ordering-by-column-value-in-sql-server-mysql/384/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

