<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; CASE Statement in ORDER BY Clause &#8211; 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>
	<lastBuildDate>Fri, 20 Nov 2009 20:17:18 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Dereck</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-56624</link>
		<dc:creator>Dereck</dc:creator>
		<pubDate>Mon, 12 Oct 2009 15:52: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-56624</guid>
		<description>Great concept!</description>
		<content:encoded><![CDATA[<p>Great concept!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: elkami</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-55528</link>
		<dc:creator>elkami</dc:creator>
		<pubDate>Wed, 02 Sep 2009 14:46:29 +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-55528</guid>
		<description>Thanks a lot, i was stuck with this problem
your code help me :D
i going to buy some beers for you :P
your blog save me a lot of times</description>
		<content:encoded><![CDATA[<p>Thanks a lot, i was stuck with this problem<br />
your code help me :D<br />
i going to buy some beers for you :P<br />
your blog save me a lot of times</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seventorrents</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-54252</link>
		<dc:creator>Seventorrents</dc:creator>
		<pubDate>Thu, 30 Jul 2009 08:00:57 +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-54252</guid>
		<description>I agree with Imran Mohammed also you can add the length of the firstname as I noticed you wand the shorter firstnames first:

Select FirstName
from #tbllogin
Where FirstName like ‘%SU%’
order by charindex ( ’su’, firstname ),Len(firstname)</description>
		<content:encoded><![CDATA[<p>I agree with Imran Mohammed also you can add the length of the firstname as I noticed you wand the shorter firstnames first:</p>
<p>Select FirstName<br />
from #tbllogin<br />
Where FirstName like ‘%SU%’<br />
order by charindex ( ’su’, firstname ),Len(firstname)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-54228</link>
		<dc:creator>Imran Mohammed</dc:creator>
		<pubDate>Thu, 30 Jul 2009 03:39:36 +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-54228</guid>
		<description>@Sumit,

Use CharIndex function to location the position of &#039;SU&#039; Character. Below is a sample code. 

create table #tbllogin (firstname varchar(200))
go
insert into #tbllogin values (&#039;Kumarsumit&#039;)
insert into #tbllogin values (&#039;sumit&#039;)
insert into #tbllogin values (&#039;Kumsumit&#039;)
insert into #tbllogin values (&#039;Ssumit&#039;)
go
Select FirstName from #tbllogin
go
Select	FirstName 
from	#tbllogin 
Where	FirstName like &#039;%SU%&#039;
order by charindex ( &#039;su&#039;, firstname )
go
drop table #tbllogin 

~ IM.</description>
		<content:encoded><![CDATA[<p>@Sumit,</p>
<p>Use CharIndex function to location the position of &#8216;SU&#8217; Character. Below is a sample code. </p>
<p>create table #tbllogin (firstname varchar(200))<br />
go<br />
insert into #tbllogin values (&#8216;Kumarsumit&#8217;)<br />
insert into #tbllogin values (&#8217;sumit&#8217;)<br />
insert into #tbllogin values (&#8216;Kumsumit&#8217;)<br />
insert into #tbllogin values (&#8216;Ssumit&#8217;)<br />
go<br />
Select FirstName from #tbllogin<br />
go<br />
Select	FirstName<br />
from	#tbllogin<br />
Where	FirstName like &#8216;%SU%&#8217;<br />
order by charindex ( &#8217;su&#8217;, firstname )<br />
go<br />
drop table #tbllogin </p>
<p>~ IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sumit</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-54203</link>
		<dc:creator>Sumit</dc:creator>
		<pubDate>Wed, 29 Jul 2009 12:17:39 +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-54203</guid>
		<description>I have on urgent query....

suppose i am using like query to search...
query may be like
select firstname  from tbllogin where firstname like &#039;%su%&#039;

what i want how can i sort data on the basis of result found and then sorted on searched value i.e &quot;su&quot;

return data will be like
firstname 
sumit
sumitkumar
kumarSumit
vermakumarsumit

i hope you get me....

please help me</description>
		<content:encoded><![CDATA[<p>I have on urgent query&#8230;.</p>
<p>suppose i am using like query to search&#8230;<br />
query may be like<br />
select firstname  from tbllogin where firstname like &#8216;%su%&#8217;</p>
<p>what i want how can i sort data on the basis of result found and then sorted on searched value i.e &#8220;su&#8221;</p>
<p>return data will be like<br />
firstname<br />
sumit<br />
sumitkumar<br />
kumarSumit<br />
vermakumarsumit</p>
<p>i hope you get me&#8230;.</p>
<p>please help me</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sevento Rrents</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-53951</link>
		<dc:creator>Sevento Rrents</dc:creator>
		<pubDate>Wed, 22 Jul 2009 10:13:00 +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-53951</guid>
		<description>Puneet, you can use this trick:

ORDER BY 
     CASE WHEN Country=&#039;US&#039; OR Country=&#039;UK&#039; THEN 0 
     ELSE 1 END
, Country

This will order the list at first by 1 for us and uk and 0 for others then reorder it by country for those with same 1 or 0</description>
		<content:encoded><![CDATA[<p>Puneet, you can use this trick:</p>
<p>ORDER BY<br />
     CASE WHEN Country=&#8217;US&#8217; OR Country=&#8217;UK&#8217; THEN 0<br />
     ELSE 1 END<br />
, Country</p>
<p>This will order the list at first by 1 for us and uk and 0 for others then reorder it by country for those with same 1 or 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timber</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-53880</link>
		<dc:creator>Timber</dc:creator>
		<pubDate>Mon, 20 Jul 2009 13:58: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-53880</guid>
		<description>Hi Pinal, works great, thx!</description>
		<content:encoded><![CDATA[<p>Hi Pinal, works great, thx!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-53201</link>
		<dc:creator>Imran Mohammed</dc:creator>
		<pubDate>Tue, 23 Jun 2009 12:29:21 +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-53201</guid>
		<description>@Puneet, 

This is usually done through Front End. They can put a default value in the combo box (drop down box). Ask your Front End Developer to implement this logic through front end.

I have seen many cases, we usually implement these things through front end.

~ IM.</description>
		<content:encoded><![CDATA[<p>@Puneet, </p>
<p>This is usually done through Front End. They can put a default value in the combo box (drop down box). Ask your Front End Developer to implement this logic through front end.</p>
<p>I have seen many cases, we usually implement these things through front end.</p>
<p>~ IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Puneet</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-53184</link>
		<dc:creator>Puneet</dc:creator>
		<pubDate>Tue, 23 Jun 2009 06:50:36 +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-53184</guid>
		<description>I have to List all the coutries from counties table order by CountryName, but I want United states and Germany to be on the top of list.</description>
		<content:encoded><![CDATA[<p>I have to List all the coutries from counties table order by CountryName, but I want United states and Germany to be on the top of list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AZ</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-51156</link>
		<dc:creator>AZ</dc:creator>
		<pubDate>Tue, 21 Apr 2009 10:39:51 +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-51156</guid>
		<description>SET NOCOUNT ON 

	IF LEN(@SortExpression) = 0
		SET @SortExpression = &#039;ID DESC&#039;

	SELECT * FROM 
	(
		SELECT 
			[ID], 
			[AddedBy], 
			[AddedDate], 
			[AmountOfSales], 
			[ModifiedBy], 
			[ModifiedDate], 		
			[...],
			ROW_NUMBER() OVER (ORDER BY CAST(@SortExpression as sql_variant)) AS RowNum
		FROM   
			[dbo].[sell_Sales] 
		WHERE  
			([UserID] = @UserID)
	) sales
WHERE sales.RowNum BETWEEN (@PageIndex*@PageSize+1) AND ((@PageIndex+1)*@PageSize)
ORDER BY CAST(@SortExpression as sql_variant)</description>
		<content:encoded><![CDATA[<p>SET NOCOUNT ON </p>
<p>	IF LEN(@SortExpression) = 0<br />
		SET @SortExpression = &#8216;ID DESC&#8217;</p>
<p>	SELECT * FROM<br />
	(<br />
		SELECT<br />
			[ID],<br />
			[AddedBy],<br />
			[AddedDate],<br />
			[AmountOfSales],<br />
			[ModifiedBy],<br />
			[ModifiedDate],<br />
			[...],<br />
			ROW_NUMBER() OVER (ORDER BY CAST(@SortExpression as sql_variant)) AS RowNum<br />
		FROM<br />
			[dbo].[sell_Sales]<br />
		WHERE<br />
			([UserID] = @UserID)<br />
	) sales<br />
WHERE sales.RowNum BETWEEN (@PageIndex*@PageSize+1) AND ((@PageIndex+1)*@PageSize)<br />
ORDER BY CAST(@SortExpression as sql_variant)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-47231</link>
		<dc:creator>SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Tue, 24 Feb 2009 12:09:50 +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-47231</guid>
		<description>[...] SQL SERVER - CASE Statement in ORDER BY Clause - ORDER BY using Variable [...]</description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER &#8211; CASE Statement in ORDER BY Clause &#8211; ORDER BY using Variable [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iNTERGALACTIC</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-46404</link>
		<dc:creator>iNTERGALACTIC</dc:creator>
		<pubDate>Fri, 06 Feb 2009 15:53: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-46404</guid>
		<description>Thank you Vassalis for answering the question and Rökkvi for providing the answer!</description>
		<content:encoded><![CDATA[<p>Thank you Vassalis for answering the question and Rökkvi for providing the answer!</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-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&#039;m a bit rust on SQL but this statement in the order by confuses me. Doesn&#039;t 3 mean the 3rd column, 2 mean the 2nd, etc...? If so why doesn&#039;t 0 throw an error? I know it doesn&#039;t but I don&#039;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 + &#039; &#039; + X.FirstName END,
		CASE WHEN @sort = 1 
				AND @sortdirection = &#039;D&#039;
			THEN X.ClientName END DESC,
		CASE WHEN @sort = 1 
				AND @sortdirection = &#039;&#039;
			THEN X.ClientName END,
		CASE WHEN @sort = 2
				AND @sortdirection = &#039;D&#039;
			THEN X.StatusTypeDesc END DESC,
		CASE WHEN @sort = 2
				AND @sortdirection = &#039;&#039;
			THEN X.StatusTypeDesc END,
		CASE WHEN @sort = 3
				AND @sortdirection = &#039;D&#039;
			THEN Convert(varchar(20),X.CreationDate) END DESC,
		CASE WHEN @sort = 3
				AND @sortdirection = &#039;&#039;
			THEN Convert(varchar(20),X.CreationDate) END,
		CASE WHEN @sort = 4
				AND @sortdirection = &#039;D&#039;
			THEN X.Name END DESC,
		CASE WHEN @sort = 4
				AND @sortdirection = &#039;&#039;
			THEN X.Name END,
		CASE WHEN @sort = 5
				AND @sortdirection = &#039;D&#039;
			THEN Convert(varchar(20),X.StartDate) END DESC,
		CASE WHEN @sort = 5
				AND @sortdirection = &#039;&#039;
			THEN Convert(varchar(20),X.StartDate) END,
		CASE WHEN @sort = 6
				AND @sortdirection = &#039;D&#039;
			THEN X.categorycode END DESC,
		CASE WHEN @sort = 6
				AND @sortdirection = &#039;&#039;
			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&#039;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 &#039;asc&#039; then
					case @SortBy
						when &#039;This&#039; then
							case [That]
								when 4 then 1 
								else 0
							end
					end
				end desc,
				case @SortOrder
					when &#039;asc&#039; then
					case @SortBy
						when &#039;This&#039; 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&#039;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 &#039;asc&#039; THEN
            CASE @SortBy
                  WHEN &#039;This&#039; THEN
                         CASE [That]
                             WHEN &#039;A&#039; THEN 3
                             WHEN &#039;B&#039; THEN 2
                             WHEN &#039;C&#039; THEN 1
                             ELSE 0
                         END, [This] --the comma doesn&#039;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 &#039;This&#039; 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&#039;t work.
I&#039;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>
</channel>
</rss>
