<?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 SERVER - CASE Statement in ORDER BY Clause - ORDER BY using Variable</title>
	<atom:link href="http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<pubDate>Wed, 07 Jan 2009 01:40:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: DJ</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44393</link>
		<dc:creator>DJ</dc:creator>
		<pubDate>Tue, 25 Nov 2008 19:09:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44393</guid>
		<description>Nvm, I just remembered that SQL treats it as a value when coming out of a CASE WHEN.</description>
		<content:encoded><![CDATA[<p>Nvm, I just remembered that SQL treats it as a value when coming out of a CASE WHEN.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DJ</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44392</link>
		<dc:creator>DJ</dc:creator>
		<pubDate>Tue, 25 Nov 2008 18:22:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44392</guid>
		<description>I'm a bit rust on SQL but this statement in the order by confuses me. Doesn't 3 mean the 3rd column, 2 mean the 2nd, etc...? If so why doesn't 0 throw an error? I know it doesn't but I don't know why.

CASE [That]
WHEN ‘A’ THEN 3
WHEN ‘B’ THEN 2
WHEN ‘C’ THEN 1
ELSE 0</description>
		<content:encoded><![CDATA[<p>I&#8217;m a bit rust on SQL but this statement in the order by confuses me. Doesn&#8217;t 3 mean the 3rd column, 2 mean the 2nd, etc&#8230;? If so why doesn&#8217;t 0 throw an error? I know it doesn&#8217;t but I don&#8217;t know why.</p>
<p>CASE [That]<br />
WHEN ‘A’ THEN 3<br />
WHEN ‘B’ THEN 2<br />
WHEN ‘C’ THEN 1<br />
ELSE 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George Nadar</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44264</link>
		<dc:creator>George Nadar</dc:creator>
		<pubDate>Tue, 18 Nov 2008 09:49:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44264</guid>
		<description>I used above solution as below...

		ORDER BY 
		CASE WHEN @sort = 0 
			THEN X.SurName + ' ' + X.FirstName END,
		CASE WHEN @sort = 1 
				AND @sortdirection = 'D'
			THEN X.ClientName END DESC,
		CASE WHEN @sort = 1 
				AND @sortdirection = ''
			THEN X.ClientName END,
		CASE WHEN @sort = 2
				AND @sortdirection = 'D'
			THEN X.StatusTypeDesc END DESC,
		CASE WHEN @sort = 2
				AND @sortdirection = ''
			THEN X.StatusTypeDesc END,
		CASE WHEN @sort = 3
				AND @sortdirection = 'D'
			THEN Convert(varchar(20),X.CreationDate) END DESC,
		CASE WHEN @sort = 3
				AND @sortdirection = ''
			THEN Convert(varchar(20),X.CreationDate) END,
		CASE WHEN @sort = 4
				AND @sortdirection = 'D'
			THEN X.Name END DESC,
		CASE WHEN @sort = 4
				AND @sortdirection = ''
			THEN X.Name END,
		CASE WHEN @sort = 5
				AND @sortdirection = 'D'
			THEN Convert(varchar(20),X.StartDate) END DESC,
		CASE WHEN @sort = 5
				AND @sortdirection = ''
			THEN Convert(varchar(20),X.StartDate) END,
		CASE WHEN @sort = 6
				AND @sortdirection = 'D'
			THEN X.categorycode END DESC,
		CASE WHEN @sort = 6
				AND @sortdirection = ''
			THEN X.categorycode END</description>
		<content:encoded><![CDATA[<p>I used above solution as below&#8230;</p>
<p>		ORDER BY<br />
		CASE WHEN @sort = 0<br />
			THEN X.SurName + &#8216; &#8216; + X.FirstName END,<br />
		CASE WHEN @sort = 1<br />
				AND @sortdirection = &#8216;D&#8217;<br />
			THEN X.ClientName END DESC,<br />
		CASE WHEN @sort = 1<br />
				AND @sortdirection = &#8221;<br />
			THEN X.ClientName END,<br />
		CASE WHEN @sort = 2<br />
				AND @sortdirection = &#8216;D&#8217;<br />
			THEN X.StatusTypeDesc END DESC,<br />
		CASE WHEN @sort = 2<br />
				AND @sortdirection = &#8221;<br />
			THEN X.StatusTypeDesc END,<br />
		CASE WHEN @sort = 3<br />
				AND @sortdirection = &#8216;D&#8217;<br />
			THEN Convert(varchar(20),X.CreationDate) END DESC,<br />
		CASE WHEN @sort = 3<br />
				AND @sortdirection = &#8221;<br />
			THEN Convert(varchar(20),X.CreationDate) END,<br />
		CASE WHEN @sort = 4<br />
				AND @sortdirection = &#8216;D&#8217;<br />
			THEN X.Name END DESC,<br />
		CASE WHEN @sort = 4<br />
				AND @sortdirection = &#8221;<br />
			THEN X.Name END,<br />
		CASE WHEN @sort = 5<br />
				AND @sortdirection = &#8216;D&#8217;<br />
			THEN Convert(varchar(20),X.StartDate) END DESC,<br />
		CASE WHEN @sort = 5<br />
				AND @sortdirection = &#8221;<br />
			THEN Convert(varchar(20),X.StartDate) END,<br />
		CASE WHEN @sort = 6<br />
				AND @sortdirection = &#8216;D&#8217;<br />
			THEN X.categorycode END DESC,<br />
		CASE WHEN @sort = 6<br />
				AND @sortdirection = &#8221;<br />
			THEN X.categorycode END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George Nadar</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44263</link>
		<dc:creator>George Nadar</dc:creator>
		<pubDate>Tue, 18 Nov 2008 09:48:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44263</guid>
		<description>This articles really helped me a lot....This solution fixed my problem....Thanks</description>
		<content:encoded><![CDATA[<p>This articles really helped me a lot&#8230;.This solution fixed my problem&#8230;.Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: towps</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44225</link>
		<dc:creator>towps</dc:creator>
		<pubDate>Fri, 14 Nov 2008 15:11:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44225</guid>
		<description>I'm wondering if anyone can help me with a cleaner implementation of this.

--note if sorting by [this] we need to sort by [that] first then by [this]
				case @SortOrder
					when 'asc' then
					case @SortBy
						when 'This' then
							case [That]
								when 4 then 1 
								else 0
							end
					end
				end desc,
				case @SortOrder
					when 'asc' then
					case @SortBy
						when 'This' then [This]
					end
				end desc,

I think that that should evaluate to 
Order by [That], [This]

Cheers folks.

Matt</description>
		<content:encoded><![CDATA[<p>I&#8217;m wondering if anyone can help me with a cleaner implementation of this.</p>
<p>&#8211;note if sorting by [this] we need to sort by [that] first then by [this]<br />
				case @SortOrder<br />
					when &#8216;asc&#8217; then<br />
					case @SortBy<br />
						when &#8216;This&#8217; then<br />
							case [That]<br />
								when 4 then 1<br />
								else 0<br />
							end<br />
					end<br />
				end desc,<br />
				case @SortOrder<br />
					when &#8216;asc&#8217; then<br />
					case @SortBy<br />
						when &#8216;This&#8217; then [This]<br />
					end<br />
				end desc,</p>
<p>I think that that should evaluate to<br />
Order by [That], [This]</p>
<p>Cheers folks.</p>
<p>Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: towps</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44212</link>
		<dc:creator>towps</dc:creator>
		<pubDate>Thu, 13 Nov 2008 20:34:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44212</guid>
		<description>Aha. I should have gone through the comments first!

I see Rökkvi has answered my question. I am eager to try!

Cheers folks.


matt.</description>
		<content:encoded><![CDATA[<p>Aha. I should have gone through the comments first!</p>
<p>I see Rökkvi has answered my question. I am eager to try!</p>
<p>Cheers folks.</p>
<p>matt.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: towps</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44211</link>
		<dc:creator>towps</dc:creator>
		<pubDate>Thu, 13 Nov 2008 20:18:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44211</guid>
		<description>Pinal.

I'm wondering if I can order by more than one column using the technique you have discussed here.

Here is an example of what I want to accomplish.

..
..
(ORDER BY
CASE @SortOrder
      WHEN 'asc' THEN
            CASE @SortBy
                  WHEN 'This' THEN
                         CASE [That]
                             WHEN 'A' THEN 3
                             WHEN 'B' THEN 2
                             WHEN 'C' THEN 1
                             ELSE 0
                         END, [This] --the comma doesn't compile
             END
END asc

Basically what I want to do is, when the user wants to sort by [This], I want to sort by [That] first using my custom specifications then sort by [This] normally.

If I just wanted to sort by [This], I would code

WHEN 'This' THEN [This]

But in my case I want to do some custom logic on [That] first then sort by [This]. The comma syntax I present there doesn't work.
I'd like to avoid having to put another case statement for each WHEN clause in the CASE [That] clause if possibke.

Just wondering if you had any suggestions, thanks!

Matt.</description>
		<content:encoded><![CDATA[<p>Pinal.</p>
<p>I&#8217;m wondering if I can order by more than one column using the technique you have discussed here.</p>
<p>Here is an example of what I want to accomplish.</p>
<p>..<br />
..<br />
(ORDER BY<br />
CASE @SortOrder<br />
      WHEN &#8216;asc&#8217; THEN<br />
            CASE @SortBy<br />
                  WHEN &#8216;This&#8217; THEN<br />
                         CASE [That]<br />
                             WHEN &#8216;A&#8217; THEN 3<br />
                             WHEN &#8216;B&#8217; THEN 2<br />
                             WHEN &#8216;C&#8217; THEN 1<br />
                             ELSE 0<br />
                         END, [This] &#8211;the comma doesn&#8217;t compile<br />
             END<br />
END asc</p>
<p>Basically what I want to do is, when the user wants to sort by [This], I want to sort by [That] first using my custom specifications then sort by [This] normally.</p>
<p>If I just wanted to sort by [This], I would code</p>
<p>WHEN &#8216;This&#8217; THEN [This]</p>
<p>But in my case I want to do some custom logic on [That] first then sort by [This]. The comma syntax I present there doesn&#8217;t work.<br />
I&#8217;d like to avoid having to put another case statement for each WHEN clause in the CASE [That] clause if possibke.</p>
<p>Just wondering if you had any suggestions, thanks!</p>
<p>Matt.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: different scott</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44069</link>
		<dc:creator>different scott</dc:creator>
		<pubDate>Wed, 05 Nov 2008 23:37:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-44069</guid>
		<description>has anybody figured out to get around the type problem. 

i know you can CAST things over to the same type, but when you cast an int over to a varchar is does whole string order. 1,11,111,2,22,222,3... I saw something mentioned about padding? i need padding on my head, but i am sure that is not what they meant. 

my types are

    3 varchars
    2 dates
    2 int

i stinks to cast all to varchars. 

thank you in advance!</description>
		<content:encoded><![CDATA[<p>has anybody figured out to get around the type problem. </p>
<p>i know you can CAST things over to the same type, but when you cast an int over to a varchar is does whole string order. 1,11,111,2,22,222,3&#8230; I saw something mentioned about padding? i need padding on my head, but i am sure that is not what they meant. </p>
<p>my types are</p>
<p>    3 varchars<br />
    2 dates<br />
    2 int</p>
<p>i stinks to cast all to varchars. </p>
<p>thank you in advance!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ursangel</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-43948</link>
		<dc:creator>Ursangel</dc:creator>
		<pubDate>Tue, 28 Oct 2008 13:47:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-43948</guid>
		<description>Pinal,
       Is it possible to give an OrderBy  like this below..

Order By 
case Column1 
         when 'IT1' then 'XXX' 
         else Column1 
end

I suppose then part should carry a column name for an order by clause.</description>
		<content:encoded><![CDATA[<p>Pinal,<br />
       Is it possible to give an OrderBy  like this below..</p>
<p>Order By<br />
case Column1<br />
         when &#8216;IT1&#8242; then &#8216;XXX&#8217;<br />
         else Column1<br />
end</p>
<p>I suppose then part should carry a column name for an order by clause.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Hoffman</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-43437</link>
		<dc:creator>Chris Hoffman</dc:creator>
		<pubDate>Wed, 01 Oct 2008 19:16:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-43437</guid>
		<description>Pinal,
This post saved my skin.  You continue to amaze and satisfy with your knowledge of SQL Server.</description>
		<content:encoded><![CDATA[<p>Pinal,<br />
This post saved my skin.  You continue to amaze and satisfy with your knowledge of SQL Server.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-43255</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Thu, 25 Sep 2008 18:11:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-43255</guid>
		<description>Here's what I use:

ORDER BY
CASE @OrderBy
  WHEN 'itemCategory' THEN itemCategory
  WHEN 'item' THEN item
END ASC,
CASE @OrderBy
  WHEN 'itemCategory DESC' THEN itemCategory
  WHEN 'item DESC' THEN item
END DESC

If you have different datatypes, don't cast as varchar since then you would have to pad strings, just a pain. When you have multiple data types, just divide the case groupings by datatype. So all the string fields would be together, all the numeric, then all the dates

Example:
ORDER BY
CASE @OrderBy
  WHEN 'itemCategory' THEN itemCategory
  WHEN 'item' THEN item
END ASC,
CASE @OrderBy
  WHEN 'someInt1' THEN someInt1
  WHEN 'someInt2' THEN someInt2
END ASC,
CASE @OrderBy
  WHEN 'itemCategory DESC' THEN itemCategory
  WHEN 'item DESC' THEN item
END DESC
CASE @OrderBy
  WHEN 'someInt1 DESC' THEN someInt1
  WHEN 'someInt2 DESC' THEN someInt2
END DESC

We have a mixed environment where we needed to support SQL 2000 and SQL 2005. The other reason that helps for sorting on the server is we use paging on the server... If you get paged data and then sort in the code retrieving it, it only sorts the paged data, which is no good to anybody.

I program in .net, and the object data source / gridviews automatically pass the sort with " DESC" as part of the string, so we checked the asc and desc @OrderBy separately as you can see.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s what I use:</p>
<p>ORDER BY<br />
CASE @OrderBy<br />
  WHEN &#8216;itemCategory&#8217; THEN itemCategory<br />
  WHEN &#8216;item&#8217; THEN item<br />
END ASC,<br />
CASE @OrderBy<br />
  WHEN &#8216;itemCategory DESC&#8217; THEN itemCategory<br />
  WHEN &#8216;item DESC&#8217; THEN item<br />
END DESC</p>
<p>If you have different datatypes, don&#8217;t cast as varchar since then you would have to pad strings, just a pain. When you have multiple data types, just divide the case groupings by datatype. So all the string fields would be together, all the numeric, then all the dates</p>
<p>Example:<br />
ORDER BY<br />
CASE @OrderBy<br />
  WHEN &#8216;itemCategory&#8217; THEN itemCategory<br />
  WHEN &#8216;item&#8217; THEN item<br />
END ASC,<br />
CASE @OrderBy<br />
  WHEN &#8217;someInt1&#8242; THEN someInt1<br />
  WHEN &#8217;someInt2&#8242; THEN someInt2<br />
END ASC,<br />
CASE @OrderBy<br />
  WHEN &#8216;itemCategory DESC&#8217; THEN itemCategory<br />
  WHEN &#8216;item DESC&#8217; THEN item<br />
END DESC<br />
CASE @OrderBy<br />
  WHEN &#8217;someInt1 DESC&#8217; THEN someInt1<br />
  WHEN &#8217;someInt2 DESC&#8217; THEN someInt2<br />
END DESC</p>
<p>We have a mixed environment where we needed to support SQL 2000 and SQL 2005. The other reason that helps for sorting on the server is we use paging on the server&#8230; If you get paged data and then sort in the code retrieving it, it only sorts the paged data, which is no good to anybody.</p>
<p>I program in .net, and the object data source / gridviews automatically pass the sort with &#8221; DESC&#8221; as part of the string, so we checked the asc and desc @OrderBy separately as you can see.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: travis</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-42969</link>
		<dc:creator>travis</dc:creator>
		<pubDate>Fri, 19 Sep 2008 02:57:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-42969</guid>
		<description>Is it possible to use the results of a case statement to create another case statement</description>
		<content:encoded><![CDATA[<p>Is it possible to use the results of a case statement to create another case statement</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rökkvi</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-42304</link>
		<dc:creator>Rökkvi</dc:creator>
		<pubDate>Tue, 02 Sep 2008 11:01:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-42304</guid>
		<description>The answer to Vasilis´question:

Use the same case multiple times in a row:

ORDER BY 
CASE WHEN @SortOrder=1 THEN Cards.Cardholder
	END,
CASE WHEN @SortOrder=1 THEN Transactions.Date
	END,
CASE WHEN @SortOrder=2 THEN Transactions.Date
	END,
	Transactions.Amount

This would in case 1 order by cardholders, date, amount
and in case 2 oder by date, amount</description>
		<content:encoded><![CDATA[<p>The answer to Vasilis´question:</p>
<p>Use the same case multiple times in a row:</p>
<p>ORDER BY<br />
CASE WHEN @SortOrder=1 THEN Cards.Cardholder<br />
	END,<br />
CASE WHEN @SortOrder=1 THEN Transactions.Date<br />
	END,<br />
CASE WHEN @SortOrder=2 THEN Transactions.Date<br />
	END,<br />
	Transactions.Amount</p>
<p>This would in case 1 order by cardholders, date, amount<br />
and in case 2 oder by date, amount</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLbeginner</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-42128</link>
		<dc:creator>SQLbeginner</dc:creator>
		<pubDate>Thu, 28 Aug 2008 14:32:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-42128</guid>
		<description>Hello Vasilis



I tried something like this ..let the case statement fall through

ORDER BY
CASE WHEN @OrderBy = ‘Option1′
THEN ORDER BY Field1  END ASC,
CASE WHEN @OrderBy = 'Option1'
THEN ORDER BY  Field2 END DESC,
CASE WHEN @OrderBy = ‘Option2′
THEN ORDER BY Field1 END  DESC,
CASE WHEN @OrderBy = ‘Option2′
THEN ORDER BY Field 2 END  DESC ,

This is obviously wrong but I need something like this.
KShah above has asked the same thing but I don’t seem to be able to find an answer.

Thanks in advance,</description>
		<content:encoded><![CDATA[<p>Hello Vasilis</p>
<p>I tried something like this ..let the case statement fall through</p>
<p>ORDER BY<br />
CASE WHEN @OrderBy = ‘Option1′<br />
THEN ORDER BY Field1  END ASC,<br />
CASE WHEN @OrderBy = &#8216;Option1&#8242;<br />
THEN ORDER BY  Field2 END DESC,<br />
CASE WHEN @OrderBy = ‘Option2′<br />
THEN ORDER BY Field1 END  DESC,<br />
CASE WHEN @OrderBy = ‘Option2′<br />
THEN ORDER BY Field 2 END  DESC ,</p>
<p>This is obviously wrong but I need something like this.<br />
KShah above has asked the same thing but I don’t seem to be able to find an answer.</p>
<p>Thanks in advance,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pyrenus</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-41306</link>
		<dc:creator>Pyrenus</dc:creator>
		<pubDate>Mon, 04 Aug 2008 14:47:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-41306</guid>
		<description>Can't use case in ORDER BY clause.  I have 2 ways to solve this problem:

1.  Use the dataview sort property to sort the data in your code.

2.  Use IF/ELSE statements in SQL server to choose the appropriate query to use.  Example:

IF @sort = 'Balance'
SELECT * from Table ORDER BY Customer.Balance
ELSE IF @Sort = 'CustomerName'
SELECT * FROM Table ORDER BY CustomerName

I use SQL Server 2000.  I do not know if this was changed in 2005.</description>
		<content:encoded><![CDATA[<p>Can&#8217;t use case in ORDER BY clause.  I have 2 ways to solve this problem:</p>
<p>1.  Use the dataview sort property to sort the data in your code.</p>
<p>2.  Use IF/ELSE statements in SQL server to choose the appropriate query to use.  Example:</p>
<p>IF @sort = &#8216;Balance&#8217;<br />
SELECT * from Table ORDER BY Customer.Balance<br />
ELSE IF @Sort = &#8216;CustomerName&#8217;<br />
SELECT * FROM Table ORDER BY CustomerName</p>
<p>I use SQL Server 2000.  I do not know if this was changed in 2005.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: h_power11</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-40635</link>
		<dc:creator>h_power11</dc:creator>
		<pubDate>Tue, 22 Jul 2008 16:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-40635</guid>
		<description>Vasilis Please Trye this

order by 
	CASE 
		WHEN @Test1 = 1 THEN 
			CASE WHEN @NestedTest2 = 1 THEN Customer.CustomersOwnNumber ELSE Customer.CustomerNumber 
			END
	END,				
	CASE WHEN @SortOrder = 2 THEN Customer.Balance END DESC, Customer.CustomerName,
	CASE WHEN @SortOrder  1 and @SortOrder  2 THEN Customer.CustomerName End</description>
		<content:encoded><![CDATA[<p>Vasilis Please Trye this</p>
<p>order by<br />
	CASE<br />
		WHEN @Test1 = 1 THEN<br />
			CASE WHEN @NestedTest2 = 1 THEN Customer.CustomersOwnNumber ELSE Customer.CustomerNumber<br />
			END<br />
	END,<br />
	CASE WHEN @SortOrder = 2 THEN Customer.Balance END DESC, Customer.CustomerName,<br />
	CASE WHEN @SortOrder  1 and @SortOrder  2 THEN Customer.CustomerName End</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Omair Ahmad</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39974</link>
		<dc:creator>Omair Ahmad</dc:creator>
		<pubDate>Wed, 09 Jul 2008 15:26:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39974</guid>
		<description>This logic does not work when you use distinct in your select statement.
Could you describer, How can we use this logic when we are using distinct in the select statement?

Thanks</description>
		<content:encoded><![CDATA[<p>This logic does not work when you use distinct in your select statement.<br />
Could you describer, How can we use this logic when we are using distinct in the select statement?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C-Lo</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39390</link>
		<dc:creator>C-Lo</dc:creator>
		<pubDate>Fri, 20 Jun 2008 14:29:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39390</guid>
		<description>Sorry didn't realize it wouldn't tab properly. /t I promise it's easier to read when properly tabbed.</description>
		<content:encoded><![CDATA[<p>Sorry didn&#8217;t realize it wouldn&#8217;t tab properly. /t I promise it&#8217;s easier to read when properly tabbed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C-Lo</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39389</link>
		<dc:creator>C-Lo</dc:creator>
		<pubDate>Fri, 20 Jun 2008 14:28:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39389</guid>
		<description>Why is this such a bulky SQL statement? You can accomplish the same thing when you change your order by like:

	ORDER BY
	CASE 
		WHEN @OrderBy = 'Postal' AND @OrderByDirection = 'D'
			THEN PostalCode DESC
		WHEN @OrderBy = 'Postal' AND @OrderByDirection != 'D'
			THEN PostalCode
		WHEN @OrderBy = 'State' AND @OrderByDirection = 'D'
			THEN StateProvinceID DESC
		WHEN @OrderBy = 'State' AND @OrderByDirection != 'D'
			THEN StateProvinceID
		WHEN @OrderBy = 'City' AND @OrderByDirection = 'D'
			THEN City DESC
		WHEN @OrderBy = 'City' AND @OrderByDirection != 'D'
			THEN City 
	END

and if you want to increase performance and readability (in my opinion) then do it this way:

	ORDER BY
	CASE @OrderByDirection
		WHEN 'D'
			CASE @OrderBy
				WHEN 'Postal'
					THEN PostalCode DESC
				WHEN 'State'
					THEN StateProvinceID DESC
				WHEN 'City'
					THEN City DESC
			END
		ELSE
			CASE @OrderBy
				WHEN 'Postal'
					THEN PostalCode
				WHEN 'State'
					THEN StateProvinceID
				WHEN 'City'
					THEN City 
			END
	END

Just a suggestion.</description>
		<content:encoded><![CDATA[<p>Why is this such a bulky SQL statement? You can accomplish the same thing when you change your order by like:</p>
<p>	ORDER BY<br />
	CASE<br />
		WHEN @OrderBy = &#8216;Postal&#8217; AND @OrderByDirection = &#8216;D&#8217;<br />
			THEN PostalCode DESC<br />
		WHEN @OrderBy = &#8216;Postal&#8217; AND @OrderByDirection != &#8216;D&#8217;<br />
			THEN PostalCode<br />
		WHEN @OrderBy = &#8216;State&#8217; AND @OrderByDirection = &#8216;D&#8217;<br />
			THEN StateProvinceID DESC<br />
		WHEN @OrderBy = &#8216;State&#8217; AND @OrderByDirection != &#8216;D&#8217;<br />
			THEN StateProvinceID<br />
		WHEN @OrderBy = &#8216;City&#8217; AND @OrderByDirection = &#8216;D&#8217;<br />
			THEN City DESC<br />
		WHEN @OrderBy = &#8216;City&#8217; AND @OrderByDirection != &#8216;D&#8217;<br />
			THEN City<br />
	END</p>
<p>and if you want to increase performance and readability (in my opinion) then do it this way:</p>
<p>	ORDER BY<br />
	CASE @OrderByDirection<br />
		WHEN &#8216;D&#8217;<br />
			CASE @OrderBy<br />
				WHEN &#8216;Postal&#8217;<br />
					THEN PostalCode DESC<br />
				WHEN &#8216;State&#8217;<br />
					THEN StateProvinceID DESC<br />
				WHEN &#8216;City&#8217;<br />
					THEN City DESC<br />
			END<br />
		ELSE<br />
			CASE @OrderBy<br />
				WHEN &#8216;Postal&#8217;<br />
					THEN PostalCode<br />
				WHEN &#8216;State&#8217;<br />
					THEN StateProvinceID<br />
				WHEN &#8216;City&#8217;<br />
					THEN City<br />
			END<br />
	END</p>
<p>Just a suggestion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: E.D.</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39194</link>
		<dc:creator>E.D.</dc:creator>
		<pubDate>Thu, 12 Jun 2008 20:05:11 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-39194</guid>
		<description>Hi,
Any answers for Vasilis' question?  I'm actually looking for exactly the same answer.  
Thanks!
--E</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Any answers for Vasilis&#8217; question?  I&#8217;m actually looking for exactly the same answer.<br />
Thanks!<br />
&#8211;E</p>
]]></content:encoded>
	</item>
</channel>
</rss>
