<?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>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 10 Feb 2012 04:35:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Brendan</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-246197</link>
		<dc:creator><![CDATA[Brendan]]></dc:creator>
		<pubDate>Tue, 31 Jan 2012 14:07:37 +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-246197</guid>
		<description><![CDATA[I always come across your posts. Thanks for the great information. Very helpful.]]></description>
		<content:encoded><![CDATA[<p>I always come across your posts. Thanks for the great information. Very helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ejaz</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-221303</link>
		<dc:creator><![CDATA[Ejaz]]></dc:creator>
		<pubDate>Mon, 19 Dec 2011 10:55:10 +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-221303</guid>
		<description><![CDATA[Sir,
I have one table in sql server, which has 2 column (i.e t1 [date time], t2 [date time])

Example of table
T1                                          T2
1-dec-11 9:00 AM             3-dec-11 10:00 AM


I need output
1-dec-11 = 15 hrs [24-9]
2-dec-11 =24hrs
3-dec-11 = 10Hrs

Please sir try to solve my problem

[email removed]]]></description>
		<content:encoded><![CDATA[<p>Sir,<br />
I have one table in sql server, which has 2 column (i.e t1 [date time], t2 [date time])</p>
<p>Example of table<br />
T1                                          T2<br />
1-dec-11 9:00 AM             3-dec-11 10:00 AM</p>
<p>I need output<br />
1-dec-11 = 15 hrs [24-9]<br />
2-dec-11 =24hrs<br />
3-dec-11 = 10Hrs</p>
<p>Please sir try to solve my problem</p>
<p>[email removed]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tomoyo</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-183371</link>
		<dc:creator><![CDATA[Tomoyo]]></dc:creator>
		<pubDate>Tue, 25 Oct 2011 14:06:07 +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-183371</guid>
		<description><![CDATA[Thank you very much Carl for your help! i&#039;ll try the tip 
thanks a lot!]]></description>
		<content:encoded><![CDATA[<p>Thank you very much Carl for your help! i&#8217;ll try the tip<br />
thanks a lot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-183139</link>
		<dc:creator><![CDATA[Carl]]></dc:creator>
		<pubDate>Tue, 25 Oct 2011 02:07: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-183139</guid>
		<description><![CDATA[If all the id&#039;s are integers, then you could possible use the Convert or Cast. 
http://blog.sqlauthority.com/2007/07/07/sql-server-convert-text-to-numbers-integer-cast-and-convert/

But yes, that is why order id column is not sequentially ordered. So if the id has a space before or after number, the order is treated differently as a true integer datatype.]]></description>
		<content:encoded><![CDATA[<p>If all the id&#8217;s are integers, then you could possible use the Convert or Cast.<br />
<a href="http://blog.sqlauthority.com/2007/07/07/sql-server-convert-text-to-numbers-integer-cast-and-convert/" rel="nofollow">http://blog.sqlauthority.com/2007/07/07/sql-server-convert-text-to-numbers-integer-cast-and-convert/</a></p>
<p>But yes, that is why order id column is not sequentially ordered. So if the id has a space before or after number, the order is treated differently as a true integer datatype.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tomoyo</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-182987</link>
		<dc:creator><![CDATA[Tomoyo]]></dc:creator>
		<pubDate>Mon, 24 Oct 2011 18:04: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-182987</guid>
		<description><![CDATA[Hi Carl thank you so much 4 your help, actually i&#039;m a jr programer so i&#039;m working in a database who other guys did and they decided to set varchar the type of the product id. 

i guess this is why it orders 1,10,100 not sequentially, but exist one way to order the ids in sequentially way? or because they&#039;re numbers and are set like char it cannot do??

thnks alot!]]></description>
		<content:encoded><![CDATA[<p>Hi Carl thank you so much 4 your help, actually i&#8217;m a jr programer so i&#8217;m working in a database who other guys did and they decided to set varchar the type of the product id. </p>
<p>i guess this is why it orders 1,10,100 not sequentially, but exist one way to order the ids in sequentially way? or because they&#8217;re numbers and are set like char it cannot do??</p>
<p>thnks alot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-182983</link>
		<dc:creator><![CDATA[Carl]]></dc:creator>
		<pubDate>Mon, 24 Oct 2011 17:50:55 +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-182983</guid>
		<description><![CDATA[Why are you using a varchar for ID field?]]></description>
		<content:encoded><![CDATA[<p>Why are you using a varchar for ID field?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tomoyo</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-182977</link>
		<dc:creator><![CDATA[Tomoyo]]></dc:creator>
		<pubDate>Mon, 24 Oct 2011 17:33: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-182977</guid>
		<description><![CDATA[thank you Carl for your time, the data type  of the ID is varchar but actually we are using correlatives to introduce de ids.
and i use &quot;select * &quot; because i need all the info not only the ID 

thanks again =)]]></description>
		<content:encoded><![CDATA[<p>thank you Carl for your time, the data type  of the ID is varchar but actually we are using correlatives to introduce de ids.<br />
and i use &#8220;select * &#8221; because i need all the info not only the ID </p>
<p>thanks again =)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-182971</link>
		<dc:creator><![CDATA[Carl]]></dc:creator>
		<pubDate>Mon, 24 Oct 2011 16:58:35 +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-182971</guid>
		<description><![CDATA[First you should never use &quot;Select * &quot;. What is the datatype of the column used in the order by clause?]]></description>
		<content:encoded><![CDATA[<p>First you should never use &#8220;Select * &#8220;. What is the datatype of the column used in the order by clause?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tomoyo</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-182962</link>
		<dc:creator><![CDATA[Tomoyo]]></dc:creator>
		<pubDate>Mon, 24 Oct 2011 16:38:30 +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-182962</guid>
		<description><![CDATA[Hello to all i&#039;m usin  this code 

select * from ARTICULOS
order by CD_ARTICULO asc 

but the result that i have is 1,10,100,125  when the select result must be 1,2,3...10....100,etc why the clause is listing the ids not secuencialy 1,23??
it would be so much apreciated your help.
Thank to you very much =)]]></description>
		<content:encoded><![CDATA[<p>Hello to all i&#8217;m usin  this code </p>
<p>select * from ARTICULOS<br />
order by CD_ARTICULO asc </p>
<p>but the result that i have is 1,10,100,125  when the select result must be 1,2,3&#8230;10&#8230;.100,etc why the clause is listing the ids not secuencialy 1,23??<br />
it would be so much apreciated your help.<br />
Thank to you very much =)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JULIAN VR</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-159134</link>
		<dc:creator><![CDATA[JULIAN VR]]></dc:creator>
		<pubDate>Thu, 18 Aug 2011 18:57:14 +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-159134</guid>
		<description><![CDATA[Thanks so much for the advice, I&#039;m big fan of your page it had saved my day many times, regards]]></description>
		<content:encoded><![CDATA[<p>Thanks so much for the advice, I&#8217;m big fan of your page it had saved my day many times, regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chuck</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-144235</link>
		<dc:creator><![CDATA[Chuck]]></dc:creator>
		<pubDate>Mon, 27 Jun 2011 19:12:26 +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-144235</guid>
		<description><![CDATA[This fixed my problem.  Thanks!]]></description>
		<content:encoded><![CDATA[<p>This fixed my problem.  Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sunil</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-138615</link>
		<dc:creator><![CDATA[sunil]]></dc:creator>
		<pubDate>Fri, 03 Jun 2011 09:56:41 +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-138615</guid>
		<description><![CDATA[Sir please give a solution/query for the below table format


hcode    tcode    datetime                               count  

0001     301       01.01.2011 12:23:34               6

0001      301      01.01.2011 12:25:45              8

0002     301         01.01.2011 12:25:49         8

002     301        01.01.2011 12:25:55               7

so from the above table i want to show like below





hcode    tcode    datetime                               count  

0001       301        01.01.201                          14
0002         301         01.01.2011                     15


where i have used count function and convert function to select date between two date to get the result as the 1st table..but i need to show like the second one..


please reply quickly...]]></description>
		<content:encoded><![CDATA[<p>Sir please give a solution/query for the below table format</p>
<p>hcode    tcode    datetime                               count  </p>
<p>0001     301       01.01.2011 12:23:34               6</p>
<p>0001      301      01.01.2011 12:25:45              8</p>
<p>0002     301         01.01.2011 12:25:49         8</p>
<p>002     301        01.01.2011 12:25:55               7</p>
<p>so from the above table i want to show like below</p>
<p>hcode    tcode    datetime                               count  </p>
<p>0001       301        01.01.201                          14<br />
0002         301         01.01.2011                     15</p>
<p>where i have used count function and convert function to select date between two date to get the result as the 1st table..but i need to show like the second one..</p>
<p>please reply quickly&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rhymie</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-127336</link>
		<dc:creator><![CDATA[rhymie]]></dc:creator>
		<pubDate>Thu, 07 Apr 2011 09:56: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-127336</guid>
		<description><![CDATA[Can i use the field selected in case statement? Need urgent info. Thanks

SELECT DISTINCT PARTNERK1.job_no as jobNo, PARTNERK1.name, SendFlatK1.Date_send,  
CASE WHEN RESPONSE.AA_REGNO = &#039;CUSERR&#039;
       THEN (SELECT AA_REGNO FROM RESP WHERE    RES_TYPE=&#039;961&#039; AND JOB_NO = PARTNERK1.job_no)
END [AA_REGNO],

CASE WHEN RESPSTT.RES_CODE = &#039;HD1&#039; 
        THEN &#039;1ST REMINDER FOR HANGING CUSTOMS FORMS&#039; 
    WHEN RESPSTT.RES_CODE = &#039;HD2&#039; 
        THEN &#039;2ND REMINDER FOR HANGING CUSTOMS FORMS&#039; 
    --ELSE &#039;CANCELLATION OF HANGING CUSTOMS FORMS&#039;
END [RES_CODE],
Response.Recv_date, 
Response.Recv_time  
--FROM PARTNERK1 INNER JOIN (SendFlatK1 INNER JOIN (Response INNER JOIN RESPSTT ON RESPONSE.Recv_SNRF = RESPSTT.Recv_SNRF)  
FROM PARTNERK1 INNER JOIN   (SendFlatK1 INNER JOIN 
                                (Response INNER JOIN RESPSTT ON RESPONSE.Recv_SNRF = RESPSTT.Recv_SNRF)  
                            ON SendFlatK1.ACCOUNT_NO = Response.ACCOUNT_NO AND SendFlatK1.job_no = Response.job_no)  
        ON PARTNERK1.account_no = SendFlatK1.account_no AND PARTNERK1.job_no = SendFlatK1.job_no  
WHERE Response.Decl_Type = &#039;K1&#039;  
AND PARTNERK1.type = &#039;1&#039;  
AND Response.account_no = &#039;177974&#039;  
AND Response.user_id = &#039;rat&#039;  
AND Response.agent_code = &#039;HF1001&#039;  
AND Response.Res_Type = &#039;963&#039;  
AND Response.Status = &#039;HD&#039;  
AND RESPSTT.RES_CODE IN (&#039;HD1&#039;,&#039;HD2&#039;)]]></description>
		<content:encoded><![CDATA[<p>Can i use the field selected in case statement? Need urgent info. Thanks</p>
<p>SELECT DISTINCT PARTNERK1.job_no as jobNo, PARTNERK1.name, SendFlatK1.Date_send,<br />
CASE WHEN RESPONSE.AA_REGNO = &#8216;CUSERR&#8217;<br />
       THEN (SELECT AA_REGNO FROM RESP WHERE    RES_TYPE=&#8217;961&#8242; AND JOB_NO = PARTNERK1.job_no)<br />
END [AA_REGNO],</p>
<p>CASE WHEN RESPSTT.RES_CODE = &#8216;HD1&#8242;<br />
        THEN &#8217;1ST REMINDER FOR HANGING CUSTOMS FORMS&#8217;<br />
    WHEN RESPSTT.RES_CODE = &#8216;HD2&#8242;<br />
        THEN &#8217;2ND REMINDER FOR HANGING CUSTOMS FORMS&#8217;<br />
    &#8211;ELSE &#8216;CANCELLATION OF HANGING CUSTOMS FORMS&#8217;<br />
END [RES_CODE],<br />
Response.Recv_date,<br />
Response.Recv_time<br />
&#8211;FROM PARTNERK1 INNER JOIN (SendFlatK1 INNER JOIN (Response INNER JOIN RESPSTT ON RESPONSE.Recv_SNRF = RESPSTT.Recv_SNRF)<br />
FROM PARTNERK1 INNER JOIN   (SendFlatK1 INNER JOIN<br />
                                (Response INNER JOIN RESPSTT ON RESPONSE.Recv_SNRF = RESPSTT.Recv_SNRF)<br />
                            ON SendFlatK1.ACCOUNT_NO = Response.ACCOUNT_NO AND SendFlatK1.job_no = Response.job_no)<br />
        ON PARTNERK1.account_no = SendFlatK1.account_no AND PARTNERK1.job_no = SendFlatK1.job_no<br />
WHERE Response.Decl_Type = &#8216;K1&#8242;<br />
AND PARTNERK1.type = &#8217;1&#8242;<br />
AND Response.account_no = &#8217;177974&#8242;<br />
AND Response.user_id = &#8216;rat&#8217;<br />
AND Response.agent_code = &#8216;HF1001&#8242;<br />
AND Response.Res_Type = &#8217;963&#8242;<br />
AND Response.Status = &#8216;HD&#8217;<br />
AND RESPSTT.RES_CODE IN (&#8216;HD1&#8242;,&#8217;HD2&#8242;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-93262</link>
		<dc:creator><![CDATA[Carl]]></dc:creator>
		<pubDate>Fri, 15 Oct 2010 15:15:02 +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-93262</guid>
		<description><![CDATA[DECLARE @dtStart DATETIME
DECLARE @dtEnd DATETIME

SET @dtStart = &#039;2010-12-14&#039;
SET @dtEnd = &#039;2010-12-22&#039;

SELECT	
	CASE WHEN MONTH(@dtStart) BETWEEN 1 AND 3 
	THEN 
		CASE WHEN MONTH(@dtEnd) BETWEEN 1 AND 3 
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)
		ELSE DATEDIFF(DAY,@dtStart,&#039;2010-03-31&#039;) END 
	WHEN MONTH(@dtStart) BETWEEN 4 AND 6	
	THEN 
		CASE WHEN MONTH(@dtEnd) BETWEEN 4 AND 6 
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)
		ELSE DATEDIFF(DAY,@dtStart,&#039;2010-06-30&#039;) END
	WHEN MONTH(@dtStart) BETWEEN 7 AND 9	
	THEN 
		CASE WHEN MONTH(@dtEnd) BETWEEN 7 AND 9 
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)
		ELSE DATEDIFF(DAY,@dtStart,&#039;2010-09-30&#039;) END
	ELSE
		CASE WHEN MONTH(@dtEnd) BETWEEN 10 AND 12 
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)
		ELSE DATEDIFF(DAY,@dtStart,&#039;2010-12-31&#039;) END
	END AS QDAYS]]></description>
		<content:encoded><![CDATA[<p>DECLARE @dtStart DATETIME<br />
DECLARE @dtEnd DATETIME</p>
<p>SET @dtStart = &#8217;2010-12-14&#8242;<br />
SET @dtEnd = &#8217;2010-12-22&#8242;</p>
<p>SELECT<br />
	CASE WHEN MONTH(@dtStart) BETWEEN 1 AND 3<br />
	THEN<br />
		CASE WHEN MONTH(@dtEnd) BETWEEN 1 AND 3<br />
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)<br />
		ELSE DATEDIFF(DAY,@dtStart,&#8217;2010-03-31&#8242;) END<br />
	WHEN MONTH(@dtStart) BETWEEN 4 AND 6<br />
	THEN<br />
		CASE WHEN MONTH(@dtEnd) BETWEEN 4 AND 6<br />
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)<br />
		ELSE DATEDIFF(DAY,@dtStart,&#8217;2010-06-30&#8242;) END<br />
	WHEN MONTH(@dtStart) BETWEEN 7 AND 9<br />
	THEN<br />
		CASE WHEN MONTH(@dtEnd) BETWEEN 7 AND 9<br />
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)<br />
		ELSE DATEDIFF(DAY,@dtStart,&#8217;2010-09-30&#8242;) END<br />
	ELSE<br />
		CASE WHEN MONTH(@dtEnd) BETWEEN 10 AND 12<br />
		THEN DATEDIFF(DAY,@dtStart,@dtEnd)<br />
		ELSE DATEDIFF(DAY,@dtStart,&#8217;2010-12-31&#8242;) END<br />
	END AS QDAYS</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bhumi Dev</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-93187</link>
		<dc:creator><![CDATA[bhumi Dev]]></dc:creator>
		<pubDate>Fri, 15 Oct 2010 08:38: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-93187</guid>
		<description><![CDATA[Sir 
can u solve this problem i want to find out number of days in 4 quater in year 
1 qtr from jan-mar
2 april to june
3 jul-sept
4 oct-dec
if i have table fields start_date and End_date
iam inserting my code 
------------
SELECT convert(char(14),start_date,103)  Start_Date,
 convert(char(14),End_date,103) End_Date, 
CASE WHEN datepart(mm,start_date)  between 1and 3 and
datepart(mm,end_date)  between 1 and 3
THEN datediff(d, end_date,start_date) ELSE
     datediff(d,&#039;2010-03-31&#039;,start_date) END as Qtr1,
--Start 2nd Quater from
CASE WHEN datepart(mm,start_date)  between 4 and 6
and
-datepart(mm,end_date)  between 4 and 6
THEN datediff(d, end_date,&#039;2010-04-01&#039;) ELSE 
     datediff(d,&#039;2010-06-30&#039;,&#039;2010-04-01&#039;) END as Qtr2,
--Start 3rd Quater from
CASE WHEN datepart(mm,start_date)  between 7 and 9
and
-datepart(mm,end_date)  between 7 and 9
THEN datediff(d, end_date,&#039;2010-07-01&#039;) ELSE 
     datediff(d,&#039;2010-09-30&#039;,&#039;2010-07-01&#039;) END as Qtr3,
--Start 4th Quater from
CASE WHEN datepart(mm,start_date)  between 10 and 12
and
-datepart(mm,end_date)  between 10 and 12
THEN datediff(d, end_date,&#039;2010-10-01&#039;) ELSE 
     datediff(d,&#039;2010-12-31&#039;,&#039;2010-10-01&#039;) END as Qtr4,
datediff(d,end_date,start_date)Actul_Days
FROM Z_FDR]]></description>
		<content:encoded><![CDATA[<p>Sir<br />
can u solve this problem i want to find out number of days in 4 quater in year<br />
1 qtr from jan-mar<br />
2 april to june<br />
3 jul-sept<br />
4 oct-dec<br />
if i have table fields start_date and End_date<br />
iam inserting my code<br />
&#8212;&#8212;&#8212;&#8212;<br />
SELECT convert(char(14),start_date,103)  Start_Date,<br />
 convert(char(14),End_date,103) End_Date,<br />
CASE WHEN datepart(mm,start_date)  between 1and 3 and<br />
datepart(mm,end_date)  between 1 and 3<br />
THEN datediff(d, end_date,start_date) ELSE<br />
     datediff(d,&#8217;2010-03-31&#8242;,start_date) END as Qtr1,<br />
&#8211;Start 2nd Quater from<br />
CASE WHEN datepart(mm,start_date)  between 4 and 6<br />
and<br />
-datepart(mm,end_date)  between 4 and 6<br />
THEN datediff(d, end_date,&#8217;2010-04-01&#8242;) ELSE<br />
     datediff(d,&#8217;2010-06-30&#8242;,&#8217;2010-04-01&#8242;) END as Qtr2,<br />
&#8211;Start 3rd Quater from<br />
CASE WHEN datepart(mm,start_date)  between 7 and 9<br />
and<br />
-datepart(mm,end_date)  between 7 and 9<br />
THEN datediff(d, end_date,&#8217;2010-07-01&#8242;) ELSE<br />
     datediff(d,&#8217;2010-09-30&#8242;,&#8217;2010-07-01&#8242;) END as Qtr3,<br />
&#8211;Start 4th Quater from<br />
CASE WHEN datepart(mm,start_date)  between 10 and 12<br />
and<br />
-datepart(mm,end_date)  between 10 and 12<br />
THEN datediff(d, end_date,&#8217;2010-10-01&#8242;) ELSE<br />
     datediff(d,&#8217;2010-12-31&#8242;,&#8217;2010-10-01&#8242;) END as Qtr4,<br />
datediff(d,end_date,start_date)Actul_Days<br />
FROM Z_FDR</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-92591</link>
		<dc:creator><![CDATA[Carl]]></dc:creator>
		<pubDate>Wed, 13 Oct 2010 18:19:31 +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-92591</guid>
		<description><![CDATA[rave: Keep it simple; if your users do not request a multi-column order by then I would not use the above solution. Another way would be to use Data binding throught Linq to SQL.]]></description>
		<content:encoded><![CDATA[<p>rave: Keep it simple; if your users do not request a multi-column order by then I would not use the above solution. Another way would be to use Data binding throught Linq to SQL.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rave</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-92571</link>
		<dc:creator><![CDATA[rave]]></dc:creator>
		<pubDate>Wed, 13 Oct 2010 16:07: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-92571</guid>
		<description><![CDATA[@Carl : How does this affect my performance than an ordinary ORDER BY clause?
Is there any other way to achive this?]]></description>
		<content:encoded><![CDATA[<p>@Carl : How does this affect my performance than an ordinary ORDER BY clause?<br />
Is there any other way to achive this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mahesh</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-91368</link>
		<dc:creator><![CDATA[mahesh]]></dc:creator>
		<pubDate>Tue, 05 Oct 2010 19:54: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-91368</guid>
		<description><![CDATA[Hi Pinal,

I want to execute a set of sql statements depending on the category selected 
to achieve this i am using case statement 

following are the statements in the stored procedure - 

case @cat
when &#039;sports&#039; then 
select * from news where type = @cat
when &#039;tech&#039; then 
select * from news where type = @cat
end


pls note - i dont want to use dynamic query 

the above query not works 
i have observed that we can&#039;t use select clause in case statement 
whereas we can use case in select statement.

Please clarify me on this 

Kind Regards,
D.Mahesh]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I want to execute a set of sql statements depending on the category selected<br />
to achieve this i am using case statement </p>
<p>following are the statements in the stored procedure &#8211; </p>
<p>case @cat<br />
when &#8216;sports&#8217; then<br />
select * from news where type = @cat<br />
when &#8216;tech&#8217; then<br />
select * from news where type = @cat<br />
end</p>
<p>pls note &#8211; i dont want to use dynamic query </p>
<p>the above query not works<br />
i have observed that we can&#8217;t use select clause in case statement<br />
whereas we can use case in select statement.</p>
<p>Please clarify me on this </p>
<p>Kind Regards,<br />
D.Mahesh</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neeraj</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-89103</link>
		<dc:creator><![CDATA[Neeraj]]></dc:creator>
		<pubDate>Thu, 23 Sep 2010 12:06:45 +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-89103</guid>
		<description><![CDATA[Good..This solved my problem.]]></description>
		<content:encoded><![CDATA[<p>Good..This solved my problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aankur singla</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-87898</link>
		<dc:creator><![CDATA[Aankur singla]]></dc:creator>
		<pubDate>Wed, 15 Sep 2010 07:57:31 +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-87898</guid>
		<description><![CDATA[ORDER BY 
    CASE –- VARCHAR types ascending 
        WHEN @OrderBy = &#039;ProductNameAsc&#039; THEN ProductName 
    END ASC, 
    CASE –- VARCHAR types descending 
        WHEN @OrderBy = &#039;ProductNameDesc&#039; THEN ProductName 
    END DESC, 
    CASE -- INT types ascending 
        WHEN @OrderBy = &#039;QuantityAsc&#039; THEN Quantity 
    END ASC, 
    CASE -- INT types descending 
        WHEN @OrderBy = &#039;QuantityDesc&#039; THEN Quantity 
    END DESC, 
    CASE -- MONEY types ascending 
         WHEN @OrderBy = &#039;PriceAsc&#039; THEN Price 
    END ASC, 
    CASE -- MONEY types descending 
        WHEN @OrderBy = &#039;PriceDesc&#039; THEN Price 
    END DESC, 
    CASE -- Default order by 
        WHEN 1 = 1 THEN ProductName 
    END ASC]]></description>
		<content:encoded><![CDATA[<p>ORDER BY<br />
    CASE –- VARCHAR types ascending<br />
        WHEN @OrderBy = &#8216;ProductNameAsc&#8217; THEN ProductName<br />
    END ASC,<br />
    CASE –- VARCHAR types descending<br />
        WHEN @OrderBy = &#8216;ProductNameDesc&#8217; THEN ProductName<br />
    END DESC,<br />
    CASE &#8212; INT types ascending<br />
        WHEN @OrderBy = &#8216;QuantityAsc&#8217; THEN Quantity<br />
    END ASC,<br />
    CASE &#8212; INT types descending<br />
        WHEN @OrderBy = &#8216;QuantityDesc&#8217; THEN Quantity<br />
    END DESC,<br />
    CASE &#8212; MONEY types ascending<br />
         WHEN @OrderBy = &#8216;PriceAsc&#8217; THEN Price<br />
    END ASC,<br />
    CASE &#8212; MONEY types descending<br />
        WHEN @OrderBy = &#8216;PriceDesc&#8217; THEN Price<br />
    END DESC,<br />
    CASE &#8212; Default order by<br />
        WHEN 1 = 1 THEN ProductName<br />
    END ASC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamal</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-87817</link>
		<dc:creator><![CDATA[Kamal]]></dc:creator>
		<pubDate>Tue, 14 Sep 2010 12:29:27 +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-87817</guid>
		<description><![CDATA[Cheers, this solved my problem after hours of googling!]]></description>
		<content:encoded><![CDATA[<p>Cheers, this solved my problem after hours of googling!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-80074</link>
		<dc:creator><![CDATA[Carl]]></dc:creator>
		<pubDate>Tue, 13 Jul 2010 12:36: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-80074</guid>
		<description><![CDATA[ORDER BY
CASE
WHEN @col=”name” AND @sort=”asc” THEN (RANK() OVER (ORDER BY Name, Status))
WHEN @col=”name” AND @sort=”desc” THEN (RANK() OVER (ORDER BY Name DESC, Status))
WHEN @col=”status” AND @sort=”asc” THEN (RANK() OVER (ORDER BY Status, Name))
WHEN @col=”status” AND @sort=”desc” THEN (RANK() OVER (ORDER BY Status DESC, Name))
WHEN @col=”” THEN (RANK() OVER (ORDER BY Name, Status))
END]]></description>
		<content:encoded><![CDATA[<p>ORDER BY<br />
CASE<br />
WHEN @col=”name” AND @sort=”asc” THEN (RANK() OVER (ORDER BY Name, Status))<br />
WHEN @col=”name” AND @sort=”desc” THEN (RANK() OVER (ORDER BY Name DESC, Status))<br />
WHEN @col=”status” AND @sort=”asc” THEN (RANK() OVER (ORDER BY Status, Name))<br />
WHEN @col=”status” AND @sort=”desc” THEN (RANK() OVER (ORDER BY Status DESC, Name))<br />
WHEN @col=”” THEN (RANK() OVER (ORDER BY Name, Status))<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wesley Baldan</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-79962</link>
		<dc:creator><![CDATA[Wesley Baldan]]></dc:creator>
		<pubDate>Mon, 12 Jul 2010 20:56:34 +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-79962</guid>
		<description><![CDATA[Try this...

ORDER BY
CASE WHEN @OrderBy = ‘Option1′
THEN ORDER BY Field1 ASC END, Field 3 ASC
CASE WHEN @OrderBy = ‘Option2′
THEN ORDER BY Field1 DESC, Field 2 DESC END,]]></description>
		<content:encoded><![CDATA[<p>Try this&#8230;</p>
<p>ORDER BY<br />
CASE WHEN @OrderBy = ‘Option1′<br />
THEN ORDER BY Field1 ASC END, Field 3 ASC<br />
CASE WHEN @OrderBy = ‘Option2′<br />
THEN ORDER BY Field1 DESC, Field 2 DESC END,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-75686</link>
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Fri, 11 Jun 2010 16:33: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-75686</guid>
		<description><![CDATA[Cool idea.  This solved my problem.

I was about to give up and code it in the application.]]></description>
		<content:encoded><![CDATA[<p>Cool idea.  This solved my problem.</p>
<p>I was about to give up and code it in the application.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mishri</title>
		<link>http://blog.sqlauthority.com/2007/07/17/sql-server-case-statement-in-order-by-clause-order-by-using-variable/#comment-64805</link>
		<dc:creator><![CDATA[Mishri]]></dc:creator>
		<pubDate>Thu, 08 Apr 2010 10:54:40 +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-64805</guid>
		<description><![CDATA[Hey,

I was just wondering if its possible to set value of two variables based on the case ??

For e.g.

 WHEN &#039;00&#039; THEN @Channel=&#039;ABC&#039;,@Status=&#039;XYZ&#039;

I am a newbee to SQL and I am not getting how to achieve this.
Please let me know the way t do it.
Any help is appreciated.

Thanks,
Mishri]]></description>
		<content:encoded><![CDATA[<p>Hey,</p>
<p>I was just wondering if its possible to set value of two variables based on the case ??</p>
<p>For e.g.</p>
<p> WHEN &#8217;00&#8242; THEN @Channel=&#8217;ABC&#8217;,@Status=&#8217;XYZ&#8217;</p>
<p>I am a newbee to SQL and I am not getting how to achieve this.<br />
Please let me know the way t do it.<br />
Any help is appreciated.</p>
<p>Thanks,<br />
Mishri</p>
]]></content:encoded>
	</item>
</channel>
</rss>

