<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>John Sheehan : Blog - Latest Comments in Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://justsayinmorewords.disqus.com/</link><description>A blog by John Sheehan</description><atom:link href="https://justsayinmorewords.disqus.com/slightly_more_dynamic_order_by_in_sql_server_2005/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 24 Jun 2010 05:02:53 -0000</lastBuildDate><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-58380978</link><description>&lt;p&gt;This is indeed a better way to order a column. Still, the difference isn't that big so I don't see why anyone should bother. Or maybe the difference is noticeable in a very large database. &lt;br&gt;Sandra @ &lt;a href="http://www.webfusion.co.uk/virtual-private-servers/" rel="nofollow noopener" target="_blank" title="http://www.webfusion.co.uk/virtual-private-servers/"&gt;Cheap VPS&lt;/a&gt;&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">SandraMillhouse</dc:creator><pubDate>Thu, 24 Jun 2010 05:02:53 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-9973887</link><description>&lt;p&gt;I forgot to mention that this way should also work in SQL Server 2000 for anyone still using that.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Miguel_TX</dc:creator><pubDate>Tue, 26 May 2009 18:51:28 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-9973786</link><description>&lt;p&gt;I just tried this way of sorting and for the query I was running (a very big one) it took twice as long to run than it did with my own dynamic sorting.&lt;/p&gt;&lt;p&gt;Everyone, please use this instead - it's faster when it counts:&lt;br&gt;    ORDER BY&lt;br&gt;         (CASE  WHEN @CustomerTypeID = 1 THEN FirstName END), &lt;br&gt;         (CASE  WHEN @CustomerTypeID = 1 THEN LastName  END),&lt;br&gt;         (CASE  WHEN @CustomerTypeID = 2 THEN LastName  END), &lt;br&gt;         (CASE  WHEN @CustomerTypeID = 2 THEN FirstName END),&lt;br&gt;         (CASE  WHEN @CustomerTypeID = 3 THEN LastName  END) DESC, &lt;br&gt;         (CASE  WHEN @CustomerTypeID = 3 THEN FirstName END) DESC&lt;/p&gt;&lt;p&gt;I ask that the author of this post (John Sheehan) try it and update this post with his findings if he finds that it works better.  Thank you.&lt;/p&gt;&lt;p&gt;I can't take credit for this way of doing it as I found it months ago on another posting - but I can't remember where.&lt;/p&gt;&lt;p&gt;                    - Miguel_TX&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Miguel_TX</dc:creator><pubDate>Tue, 26 May 2009 18:47:28 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-7452326</link><description>&lt;p&gt;Sorry, I don't know off hand. Try &lt;a href="http://stackoverflow.com" rel="nofollow noopener" target="_blank" title="stackoverflow.com"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John Sheehan</dc:creator><pubDate>Mon, 23 Mar 2009 17:02:09 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-7430674</link><description>&lt;p&gt;How can I use&lt;/p&gt;&lt;p&gt;ORDER BY &lt;br&gt;CASE @AccountOperator&lt;br&gt;WHEN '&amp;gt;' THEN UserLastName&lt;br&gt;END ASC,&lt;br&gt;CASE @AccountOperator&lt;br&gt;WHEN '&amp;lt;' THEN UserFirstName&lt;br&gt;END DESC&lt;/p&gt;&lt;p&gt;when UserLastName is an alias?(without using sub query)&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">li</dc:creator><pubDate>Mon, 23 Mar 2009 04:00:34 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-6202153</link><description>&lt;p&gt;Great work, thanks!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Alexander</dc:creator><pubDate>Thu, 12 Feb 2009 02:49:41 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-5115721</link><description>&lt;p&gt;Thanks for the article. &lt;br&gt;Just a quick question. Is it possible to write this dynamic Order By with DISTINCT, as I am getting error with DISTINCT in my select statement. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Mab</dc:creator><pubDate>Wed, 14 Jan 2009 12:50:16 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-5047593</link><description>&lt;p&gt;An other alternative work around is not varchar columns casting sql_variant&lt;br&gt;fitCount is int&lt;/p&gt;&lt;p&gt;elect * from tFittest&lt;br&gt;declare @sortCriteria varchar(50) set @sortCriteria = 'name'&lt;br&gt;declare @sortDirection varchar(4) set @sortDirection = 'desc'&lt;/p&gt;&lt;p&gt;select fitCount, name, wwid, @sortCriteria, @sortDirection, cast(fitCount as sql_variant)&lt;br&gt;  from tFittest&lt;br&gt; order by &lt;br&gt;  case when @sortDirection = 'ASC' then&lt;br&gt;   case when @sortCriteria = 'fitCount' then (cast(fitCount as sql_variant) )&lt;br&gt;      when @sortCriteria = 'name' then (name )&lt;br&gt;   end&lt;br&gt; end ASC,&lt;br&gt; case when @sortDirection = 'DESC' then&lt;br&gt;   case when @sortCriteria = 'fitCount' then (cast(fitCount as sql_variant) )&lt;br&gt;      when @sortCriteria = 'name' then (name )&lt;br&gt;   end&lt;br&gt; end DESC&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">john</dc:creator><pubDate>Sat, 10 Jan 2009 15:49:15 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-3880093</link><description>&lt;p&gt;The great job......................easily explained&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Rash</dc:creator><pubDate>Tue, 18 Nov 2008 12:52:06 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139968</link><description>&lt;p&gt;Thank you soooo much. You saved my life! :)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Uncle</dc:creator><pubDate>Thu, 07 Feb 2008 11:48:28 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139967</link><description>&lt;p&gt;Great tip, thanks !&lt;/p&gt;&lt;p&gt;About your sentence : "CASE kind of works, but its limited (only one column, no sort directions, datatype issues, etc)" I just want to say that you can control the direction with multiple CASE statements :&lt;br&gt;&lt;code&gt;&lt;br&gt;Order By &lt;br&gt;   Case @orderby&lt;br&gt;     when 'DATEASC' then StartDate&lt;br&gt;     when 'NAMEASC' then LastName + FirstName&lt;br&gt;   end ASC,&lt;br&gt;   Case @orderby&lt;br&gt;      when 'DATEDESC' then StartDate&lt;br&gt;      when 'NAMEDESC' then LastName+ FirstName&lt;br&gt;   end DESC&lt;br&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Also, I didn't run into any datatype problem in with the CASE, but I only tried with nvarchars and datetime, so I'm not sure about this.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">demvin01</dc:creator><pubDate>Sat, 10 Nov 2007 23:05:40 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139966</link><description>&lt;p&gt;Excellent way.- Thanks&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Francisco</dc:creator><pubDate>Tue, 02 Oct 2007 08:46:45 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139965</link><description>&lt;p&gt;Great thanks!!! Very powerfull, clear and intelligence solution.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">RedLine</dc:creator><pubDate>Tue, 28 Aug 2007 05:14:32 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139964</link><description>&lt;p&gt;Very nice work.  A fun trick to look at too!  Thanks for sharing...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">rob</dc:creator><pubDate>Sun, 22 Jul 2007 14:24:48 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139963</link><description>&lt;p&gt;Hi Ken,&lt;br&gt;I kind of thought that would be the case for larger queries. I'm only using this method on a query for under 100 rows so I haven't run into any performance issues yet.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John Sheehan</dc:creator><pubDate>Thu, 19 Jul 2007 10:02:28 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139962</link><description>&lt;p&gt;This is a neat way of doing dynamic sorting but performance on my query is very bad with this method.  Granted it is a large query.  But other methods out perform this.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ken</dc:creator><pubDate>Thu, 19 Jul 2007 09:37:58 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139961</link><description>&lt;p&gt;Absolute magic - very well explained and saved me a real headache. Thanks a million!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Steve</dc:creator><pubDate>Thu, 05 Jul 2007 06:47:58 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139960</link><description>&lt;p&gt;Excellent post! A very clean way of writing SPs with dynamic order by.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Sumit Thomas</dc:creator><pubDate>Thu, 28 Jun 2007 09:56:22 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139959</link><description>&lt;p&gt;I like it Lamprey!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John Sheehan</dc:creator><pubDate>Wed, 02 May 2007 15:13:06 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139958</link><description>&lt;p&gt;(NOTE: If you want to output the RANK() result and order by the same value, you have to specify the whole line twice as SQL server doesnâ€™t appear to be accepting dynamically created columns as ORDER BY parameters). Here are the results:&lt;/p&gt;&lt;p&gt;Almost, you can get the rank back without specifing it twice:&lt;/p&gt;&lt;p&gt;CREATE PROCEDURE CustomerGetFinal&lt;br&gt;    @CustomerTypeID int&lt;br&gt;AS&lt;br&gt;BEGIN&lt;br&gt;    SELECT&lt;br&gt;        CustomerID,&lt;br&gt;        FirstName,&lt;br&gt;        LastName,&lt;br&gt;        CustomerTypeID,&lt;br&gt;        CASE&lt;br&gt;            WHEN @CustomerTypeID = 1 THEN (RANK() OVER (ORDER BY FirstName, LastName))&lt;br&gt;            WHEN @CustomerTypeID = 2 THEN (RANK() OVER (ORDER BY LastName, FirstName))&lt;br&gt;            WHEN @CustomerTypeID = 3 THEN (RANK() OVER (ORDER BY LastName DESC, FirstName DESC))&lt;br&gt;        END AS RankNumber&lt;br&gt;    FROM&lt;br&gt;        Customer&lt;br&gt;    WHERE&lt;br&gt;        CustomerTypeID = @CustomerTypeID&lt;br&gt;    ORDER BY&lt;br&gt;        RankNumber&lt;br&gt;END&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lamprey</dc:creator><pubDate>Wed, 02 May 2007 15:10:46 -0000</pubDate></item><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://johnsheehan.me/blog/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139957</link><description>&lt;p&gt;This is a great post... I have been trying to figure out a good way to do this forever.  Good work.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jared Roberts</dc:creator><pubDate>Thu, 26 Apr 2007 01:19:26 -0000</pubDate></item></channel></rss>