<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: SQL Alphabetical Sorting Without &quot;The&quot;</title>
	<atom:link href="http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/</link>
	<description>Chris McAvoy likes kites</description>
	<lastBuildDate>Wed, 08 Feb 2012 19:11:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: johnnnnnnn</title>
		<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/comment-page-1/#comment-236</link>
		<dc:creator>johnnnnnnn</dc:creator>
		<pubDate>Wed, 02 May 2007 04:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/#comment-236</guid>
		<description>What database are you using? If you&#039;re using PostgreSQL, you can do the extra-column solution without actually creating an extra column: create a functional index. Then include the function in your select, and it hits the index instead.

I&#039;m not sure how that would work through Django, though i do know if you have to drop to SQL, you can still create a QuerySet object from it.</description>
		<content:encoded><![CDATA[<p>What database are you using? If you&#8217;re using PostgreSQL, you can do the extra-column solution without actually creating an extra column: create a functional index. Then include the function in your select, and it hits the index instead.</p>
<p>I&#8217;m not sure how that would work through Django, though i do know if you have to drop to SQL, you can still create a QuerySet object from it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alphabetical Sorting SQL Without &#8220;The&#8221; - Push cx</title>
		<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/comment-page-1/#comment-235</link>
		<dc:creator>Alphabetical Sorting SQL Without &#8220;The&#8221; - Push cx</dc:creator>
		<pubDate>Tue, 01 May 2007 23:10:40 +0000</pubDate>
		<guid isPermaLink="false">http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/#comment-235</guid>
		<description>[...] Chris McAvoy asked how to sort alphabetically so that entries starting with &#8220;A&#8221;, &#8220;An&#8221;, or &#8220;The&#8221; end up in the proper place instead of jumbled into the As and Ts.   Do I create a field â€œname_without_theâ€ and select * from bands order by name_without_the? Iâ€™m assuming this is a common alpha-issue thatâ€™s been solved millions of times, but I canâ€™t find a best-practice suggestion via two minutes of Googling. [...]</description>
		<content:encoded><![CDATA[<p>[...] Chris McAvoy asked how to sort alphabetically so that entries starting with &#8220;A&#8221;, &#8220;An&#8221;, or &#8220;The&#8221; end up in the proper place instead of jumbled into the As and Ts.   Do I create a field â€œname_without_theâ€ and select * from bands order by name_without_the? Iâ€™m assuming this is a common alpha-issue thatâ€™s been solved millions of times, but I canâ€™t find a best-practice suggestion via two minutes of Googling. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris McAvoy</title>
		<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/comment-page-1/#comment-237</link>
		<dc:creator>Chris McAvoy</dc:creator>
		<pubDate>Tue, 01 May 2007 14:41:17 +0000</pubDate>
		<guid isPermaLink="false">http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/#comment-237</guid>
		<description>As much as I hate to do it, I think a non-the column is the solution that makes the most sense to me.  It does mean data duplication, but in a read-heavy write-light database I think that it isn&#039;t as big a deal.  The non-stated part of this whole discussion is that the app is running on Django, so the pain of dropping to pure SQL rather than putting in a &quot;strip the / a / an&quot; hook pre-save seems like it might not be worth it.  That said, thanks for the SQL lesson, I&#039;ve never tried to put conditionals in queries, other than basic WHERE stuff.  Thanks.

Chris</description>
		<content:encoded><![CDATA[<p>As much as I hate to do it, I think a non-the column is the solution that makes the most sense to me.  It does mean data duplication, but in a read-heavy write-light database I think that it isn&#8217;t as big a deal.  The non-stated part of this whole discussion is that the app is running on Django, so the pain of dropping to pure SQL rather than putting in a &#8220;strip the / a / an&#8221; hook pre-save seems like it might not be worth it.  That said, thanks for the SQL lesson, I&#8217;ve never tried to put conditionals in queries, other than basic WHERE stuff.  Thanks.</p>
<p>Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Harkins</title>
		<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/comment-page-1/#comment-240</link>
		<dc:creator>Peter Harkins</dc:creator>
		<pubDate>Tue, 01 May 2007 01:27:00 +0000</pubDate>
		<guid isPermaLink="false">http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/#comment-240</guid>
		<description>SELECT name FROM bands
ORDER BY CASE
    WHEN LOWER(LEFT(name, 2)) =&quot;a &quot;   THEN SUBSTRING(name, 3)
    WHEN LOWER(LEFT(name, 3)) =&quot;an &quot;  THEN SUBSTRING(name, 4)
    WHEN LOWER(LEFT(name, 4)) =&quot;the &quot; THEN SUBSTRING(name, 5)
    ELSE name
END;

If this isn&#039;t fast enough (and it likely won&#039;t be for a gigantic list because it means scanning every row instead of using an index), definitely create a separate column. I might not create &#039;name_without_the&#039;, I might create &#039;name_article&#039; and let my app split off the leading article. It&#039;s generally bad to have interdependent columns, but I think I like it a little more than partial data duplication. I&#039;d have to ponder it a bit.</description>
		<content:encoded><![CDATA[<p>SELECT name FROM bands<br />
ORDER BY CASE<br />
    WHEN LOWER(LEFT(name, 2)) =&#8221;a &#8221;   THEN SUBSTRING(name, 3)<br />
    WHEN LOWER(LEFT(name, 3)) =&#8221;an &#8221;  THEN SUBSTRING(name, 4)<br />
    WHEN LOWER(LEFT(name, 4)) =&#8221;the &#8221; THEN SUBSTRING(name, 5)<br />
    ELSE name<br />
END;</p>
<p>If this isn&#8217;t fast enough (and it likely won&#8217;t be for a gigantic list because it means scanning every row instead of using an index), definitely create a separate column. I might not create &#8216;name_without_the&#8217;, I might create &#8216;name_article&#8217; and let my app split off the leading article. It&#8217;s generally bad to have interdependent columns, but I think I like it a little more than partial data duplication. I&#8217;d have to ponder it a bit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Morrison</title>
		<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/comment-page-1/#comment-239</link>
		<dc:creator>Rick Morrison</dc:creator>
		<pubDate>Tue, 01 May 2007 00:42:49 +0000</pubDate>
		<guid isPermaLink="false">http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/#comment-239</guid>
		<description>Most databases have a string function that performs similarly to Python&#039;s string.replace()

Just order by an expression using this function to remove the &quot;The&quot;.</description>
		<content:encoded><![CDATA[<p>Most databases have a string function that performs similarly to Python&#8217;s string.replace()</p>
<p>Just order by an expression using this function to remove the &#8220;The&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Fein</title>
		<link>http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/comment-page-1/#comment-238</link>
		<dc:creator>Peter Fein</dc:creator>
		<pubDate>Tue, 01 May 2007 00:19:52 +0000</pubDate>
		<guid isPermaLink="false">http://weblog.lonelylion.com/2007/04/30/sql-alphabetical-sorting-without-the/#comment-238</guid>
		<description>Oh god, let&#039;s see if I can remember how to do such a thing...

SELECT name, (IF name ILIKE &#039;the *&#039; THEN name[4:] ELSE name) as sort_name ORDER BY sort_name

the name[4:] is obviously python &amp; there&#039;s probably a faster way than ILIKE &amp; you may need to stick the IF clause in the order by, but that&#039;s the gist.</description>
		<content:encoded><![CDATA[<p>Oh god, let&#8217;s see if I can remember how to do such a thing&#8230;</p>
<p>SELECT name, (IF name ILIKE &#8216;the *&#8217; THEN name[4:] ELSE name) as sort_name ORDER BY sort_name</p>
<p>the name[4:] is obviously python &amp; there&#8217;s probably a faster way than ILIKE &amp; you may need to stick the IF clause in the order by, but that&#8217;s the gist.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: weblog.lonelylion.com @ 2012-02-10 04:38:05 -->
