<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Just Sayin' More Words - 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><language>en</language><lastBuildDate>Tue, 26 May 2009 18:51:28 -0000</lastBuildDate><item><title>Re: Slightly more dynamic ORDER BY in SQL Server 2005</title><link>http://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-9973887</link><description>I forgot to mention that this way should also work in SQL Server 2000 for anyone still using that.</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-9973786</link><description>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;br&gt;&lt;br&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;br&gt;&lt;br&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;br&gt;&lt;br&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;br&gt;&lt;br&gt;                    - Miguel_TX</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-7452326</link><description>Sorry, I don't know off hand. Try &lt;a href="http://stackoverflow.com" rel="nofollow"&gt;stackoverflow.com&lt;/a&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">johnsheehan</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-7430674</link><description>How can I use&lt;br&gt; &lt;br&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;br&gt;&lt;br&gt;when UserLastName is an alias?(without using sub query)</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-6202153</link><description>Great work, thanks!</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-5115721</link><description>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.</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-5047593</link><description>An other alternative work around is not varchar columns casting sql_variant&lt;br&gt;fitCount is int&lt;br&gt;&lt;br&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;br&gt;&lt;br&gt;&lt;br&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</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-3880093</link><description>The great job......................easily explained</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139968</link><description>Thank you soooo much. You saved my life! :)</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139967</link><description>Great tip, thanks !&lt;br&gt;&lt;br&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;br&gt;&lt;br&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.</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139966</link><description>Excellent way.- Thanks</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139965</link><description>Great thanks!!! Very powerfull, clear and intelligence solution.</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139964</link><description>Very nice work.  A fun trick to look at too!  Thanks for sharing...</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139963</link><description>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.</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139962</link><description>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.</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139961</link><description>Absolute magic - very well explained and saved me a real headache. Thanks a million!</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139960</link><description>Excellent post! A very clean way of writing SPs with dynamic order by.</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139959</link><description>I like it Lamprey!</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139958</link><description>(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;br&gt;&lt;br&gt;Almost, you can get the rank back without specifing it twice:&lt;br&gt;&lt;br&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</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://john-sheehan.com/blog/index.php/slightly-more-dynamic-order-by-in-sql-server-2005/#comment-2139957</link><description>This is a great post... I have been trying to figure out a good way to do this forever.  Good work.</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>