<?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/Expression Examples and Explanation</title>
	<atom:link href="http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 10 Feb 2012 02:12:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-191489</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Wed, 09 Nov 2011 09:42:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-191489</guid>
		<description><![CDATA[Is this the full code are you using? It does not have any character like &gt;]]></description>
		<content:encoded><![CDATA[<p>Is this the full code are you using? It does not have any character like &gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marie</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-191019</link>
		<dc:creator><![CDATA[Marie]]></dc:creator>
		<pubDate>Tue, 08 Nov 2011 18:12:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-191019</guid>
		<description><![CDATA[I keep getting an error message when I use this example:  &quot;Incorrect syntax near &#039;&gt;&#039; &quot;


Example:
DECLARE @TestVal INT
SET @TestVal = 5
SELECT
CASE
WHEN @TestVal &lt;=3 THEN &#039;Top 3&#039;
ELSE &#039;Other&#039;
END]]></description>
		<content:encoded><![CDATA[<p>I keep getting an error message when I use this example:  &#8220;Incorrect syntax near &#8216;&gt;&#8217; &#8221;</p>
<p>Example:<br />
DECLARE @TestVal INT<br />
SET @TestVal = 5<br />
SELECT<br />
CASE<br />
WHEN @TestVal &lt;=3 THEN &#039;Top 3&#039;<br />
ELSE &#039;Other&#039;<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marie</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-191018</link>
		<dc:creator><![CDATA[Marie]]></dc:creator>
		<pubDate>Tue, 08 Nov 2011 18:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-191018</guid>
		<description><![CDATA[On this example, I keep getting &quot;Incorrect syntax near &#039;&gt;&#039; &quot;

Example:
DECLARE @TestVal INT
SET @TestVal = 5
SELECT
CASE
WHEN @TestVal &lt;=3 THEN &#039;Top 3&#039;
ELSE &#039;Other&#039;
END]]></description>
		<content:encoded><![CDATA[<p>On this example, I keep getting &#8220;Incorrect syntax near &#8216;&gt;&#8217; &#8221;</p>
<p>Example:<br />
DECLARE @TestVal INT<br />
SET @TestVal = 5<br />
SELECT<br />
CASE<br />
WHEN @TestVal &lt;=3 THEN &#039;Top 3&#039;<br />
ELSE &#039;Other&#039;<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dreamguy</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-190781</link>
		<dc:creator><![CDATA[dreamguy]]></dc:creator>
		<pubDate>Tue, 08 Nov 2011 11:14:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-190781</guid>
		<description><![CDATA[I want to retrieve the result based on the filter values if i pass o then i shud get all join result or if i pass filtered value then filtered result.But I am passing multiple values through parameter.
Ex: customerId may have (0,1,2) like that.In that case how to write the where condition.If i dont select any value just join it


ALTER PROCEDURE [dbo].[proc_get_akimbo_deails]
--@pi_country_id int,
--@pi_entity_id int,	
@pi_customer_id int
--@pi_event_id int,
--@pi_cpgid int
--@from_date date,
--@to_date date
AS
BEGIN
	 
	
	select dh.akimbo_hid,cou.country_name,en.entity_name, c.customer_name,e.event_name,p.promo_type,dt.baseline,dt.co_packing,dt.pbi,dt.roi,dt.scc 
	from akimbo_data_header dh 
	inner join akimbo_customer_lookup c on c.customer_id=dh.customer_id
	inner join akimbo_entity_lookup en on en.entity_id=c.entity_id 
		inner join akimbo_event_lookup e on e.event_id=dh.event_id
		inner join akimbo_country_lookup cou on cou.country_id=en.country_id
	inner join akimbo_data_transaction dt on dh.akimbo_hid=dt.akimbo_hid
	

	inner join akimbo_cpg_lookup cp on cp.cpg_id=dt.cpg_id 
	inner join akimbo_promo_type_lookup p on p.promo_type_id=dt.promo_type_id	
	

     where dh.customer_id =case when dh.customer_id in(@pi_customer_id) then  dh.customer_id in (@pi_customer_id) else dh.customer_id end
       
	group by dh.akimbo_hid,cou.country_name,en.entity_name,
	c.customer_name,e.event_name,p.promo_type,dt.baseline,dt.co_packing,dt.pbi,dt.roi,dt.scc 
	order  by cou.country_name
	
	

	 
END]]></description>
		<content:encoded><![CDATA[<p>I want to retrieve the result based on the filter values if i pass o then i shud get all join result or if i pass filtered value then filtered result.But I am passing multiple values through parameter.<br />
Ex: customerId may have (0,1,2) like that.In that case how to write the where condition.If i dont select any value just join it</p>
<p>ALTER PROCEDURE [dbo].[proc_get_akimbo_deails]<br />
&#8211;@pi_country_id int,<br />
&#8211;@pi_entity_id int,<br />
@pi_customer_id int<br />
&#8211;@pi_event_id int,<br />
&#8211;@pi_cpgid int<br />
&#8211;@from_date date,<br />
&#8211;@to_date date<br />
AS<br />
BEGIN</p>
<p>	select dh.akimbo_hid,cou.country_name,en.entity_name, c.customer_name,e.event_name,p.promo_type,dt.baseline,dt.co_packing,dt.pbi,dt.roi,dt.scc<br />
	from akimbo_data_header dh<br />
	inner join akimbo_customer_lookup c on c.customer_id=dh.customer_id<br />
	inner join akimbo_entity_lookup en on en.entity_id=c.entity_id<br />
		inner join akimbo_event_lookup e on e.event_id=dh.event_id<br />
		inner join akimbo_country_lookup cou on cou.country_id=en.country_id<br />
	inner join akimbo_data_transaction dt on dh.akimbo_hid=dt.akimbo_hid</p>
<p>	inner join akimbo_cpg_lookup cp on cp.cpg_id=dt.cpg_id<br />
	inner join akimbo_promo_type_lookup p on p.promo_type_id=dt.promo_type_id	</p>
<p>     where dh.customer_id =case when dh.customer_id in(@pi_customer_id) then  dh.customer_id in (@pi_customer_id) else dh.customer_id end</p>
<p>	group by dh.akimbo_hid,cou.country_name,en.entity_name,<br />
	c.customer_name,e.event_name,p.promo_type,dt.baseline,dt.co_packing,dt.pbi,dt.roi,dt.scc<br />
	order  by cou.country_name</p>
<p>END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-149607</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Wed, 20 Jul 2011 13:54:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-149607</guid>
		<description><![CDATA[What is the datatype of @module?]]></description>
		<content:encoded><![CDATA[<p>What is the datatype of @module?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abegailabundo</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-149563</link>
		<dc:creator><![CDATA[abegailabundo]]></dc:creator>
		<pubDate>Wed, 20 Jul 2011 09:03:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-149563</guid>
		<description><![CDATA[Hi Mani,

Please check this solution http://blog.sqlauthority.com/2007/07/08/sql-server-fix-error-msg-244-level-16-state-1-fix-error-msg-245-level-16-state-1/]]></description>
		<content:encoded><![CDATA[<p>Hi Mani,</p>
<p>Please check this solution <a href="http://blog.sqlauthority.com/2007/07/08/sql-server-fix-error-msg-244-level-16-state-1-fix-error-msg-245-level-16-state-1/" rel="nofollow">http://blog.sqlauthority.com/2007/07/08/sql-server-fix-error-msg-244-level-16-state-1-fix-error-msg-245-level-16-state-1/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mani</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-149359</link>
		<dc:creator><![CDATA[mani]]></dc:creator>
		<pubDate>Tue, 19 Jul 2011 14:22:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-149359</guid>
		<description><![CDATA[In table module is Integer datatype.

select
 case @module
when &#039;None&#039; then 0
when &#039;Students&#039; then 1
when &#039;EMedExam&#039; then 2
when &#039;ARAP&#039; then 3
when &#039;OutReach&#039; then 4
when &#039;Referrals&#039; then 5
when &#039;EmpPortal&#039; then 6 end 
from SCTASKTABLE

Im facing error conversion failed varchar to int

please help me can any one send solution to my emailid

[email removed]

Thanks in Advance]]></description>
		<content:encoded><![CDATA[<p>In table module is Integer datatype.</p>
<p>select<br />
 case @module<br />
when &#8216;None&#8217; then 0<br />
when &#8216;Students&#8217; then 1<br />
when &#8216;EMedExam&#8217; then 2<br />
when &#8216;ARAP&#8217; then 3<br />
when &#8216;OutReach&#8217; then 4<br />
when &#8216;Referrals&#8217; then 5<br />
when &#8216;EmpPortal&#8217; then 6 end<br />
from SCTASKTABLE</p>
<p>Im facing error conversion failed varchar to int</p>
<p>please help me can any one send solution to my emailid</p>
<p>[email removed]</p>
<p>Thanks in Advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Punia</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-144217</link>
		<dc:creator><![CDATA[Punia]]></dc:creator>
		<pubDate>Mon, 27 Jun 2011 16:01:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-144217</guid>
		<description><![CDATA[Hi All Experts.
I came up with some problem that I am trying to solve, but no luck. Any help will be appreciated.

Claim_id    cycle   First_date  first_rank  Second_date second rank  300 1   2011-02-25  0-13    2011-03-10  14-27 300 2   2010-09-15  14-27   2010-10-01  0-13 301 1   2011-04-14  14-27   2011-06-20  14-27 301 2   2010-08-04  14-27   2010-08-20  0-13 403 1   2011-02-09  14-27   2011-02-24  0-13 403 2   2010-08-13  28+ NULL    NULL 404 1   2011-06-16  14-27   NULL    NULL 404 2   2011-02-25  0-13    2011-03-08  14-27 404 3   2010-10-21  28+ 2010-11-16  14-27 405 1   2011-06-11  14-27   NULL    NULL 405 2   2011-03-31  28+ NULL    NULL 405 3   2010-08-10  28+ 2010-09-20  14-27 Following conditions are required:

when first rank is 0-13 than records for cycle 1 (no matter what the second rank is) for that claim_id

when first_rank is other than &#039;0-13&#039; and second_rank is not null than records of cycle 1 is needed for that claimid

when first_rank is other than &#039;0-13&#039; and second_rank is null than next cycle record is needed for that claimid till second_rank is not null .

Below is the required result:

Claim_id    cycle   First_date  first_rank  Second_date second rank  300 1   2011-02-25  0-13    2011-03-10  14-27 301 1   2011-04-14  14-27   2011-06-20  14-27 403 1   2011-02-09  14-27   2011-02-24  0-13 404 2   2011-02-25  0-13    2011-03-08  14-27 405 3   2010-08-10  28+ 2010-09-20  14-27]]></description>
		<content:encoded><![CDATA[<p>Hi All Experts.<br />
I came up with some problem that I am trying to solve, but no luck. Any help will be appreciated.</p>
<p>Claim_id    cycle   First_date  first_rank  Second_date second rank  300 1   2011-02-25  0-13    2011-03-10  14-27 300 2   2010-09-15  14-27   2010-10-01  0-13 301 1   2011-04-14  14-27   2011-06-20  14-27 301 2   2010-08-04  14-27   2010-08-20  0-13 403 1   2011-02-09  14-27   2011-02-24  0-13 403 2   2010-08-13  28+ NULL    NULL 404 1   2011-06-16  14-27   NULL    NULL 404 2   2011-02-25  0-13    2011-03-08  14-27 404 3   2010-10-21  28+ 2010-11-16  14-27 405 1   2011-06-11  14-27   NULL    NULL 405 2   2011-03-31  28+ NULL    NULL 405 3   2010-08-10  28+ 2010-09-20  14-27 Following conditions are required:</p>
<p>when first rank is 0-13 than records for cycle 1 (no matter what the second rank is) for that claim_id</p>
<p>when first_rank is other than &#8217;0-13&#8242; and second_rank is not null than records of cycle 1 is needed for that claimid</p>
<p>when first_rank is other than &#8217;0-13&#8242; and second_rank is null than next cycle record is needed for that claimid till second_rank is not null .</p>
<p>Below is the required result:</p>
<p>Claim_id    cycle   First_date  first_rank  Second_date second rank  300 1   2011-02-25  0-13    2011-03-10  14-27 301 1   2011-04-14  14-27   2011-06-20  14-27 403 1   2011-02-09  14-27   2011-02-24  0-13 404 2   2011-02-25  0-13    2011-03-08  14-27 405 3   2010-08-10  28+ 2010-09-20  14-27</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Noah</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-144055</link>
		<dc:creator><![CDATA[Noah]]></dc:creator>
		<pubDate>Mon, 27 Jun 2011 02:50:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-144055</guid>
		<description><![CDATA[Hello everyone! This message could not be written better! Reading this post reminds me of my roommate before! He always kept talking about it. I will send this article to him. Pretty sure he will enjoy reading. Thank you for sharing!]]></description>
		<content:encoded><![CDATA[<p>Hello everyone! This message could not be written better! Reading this post reminds me of my roommate before! He always kept talking about it. I will send this article to him. Pretty sure he will enjoy reading. Thank you for sharing!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zain</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-131767</link>
		<dc:creator><![CDATA[Zain]]></dc:creator>
		<pubDate>Thu, 28 Apr 2011 14:13:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-131767</guid>
		<description><![CDATA[Hi,

I got three separate T-SQL scripts to run.  Example

search emp table for code

select code,fname,lname from emp where fname =@lookfor

if code = &#039;A&#039;
select * from emp
This is just an example i got more line sof tsql code to run if code =&#039;a&#039;.

if code = &#039;b&#039;
select @ from dept

if code = &#039;c&#039;
select  * from add]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I got three separate T-SQL scripts to run.  Example</p>
<p>search emp table for code</p>
<p>select code,fname,lname from emp where fname =@lookfor</p>
<p>if code = &#8216;A&#8217;<br />
select * from emp<br />
This is just an example i got more line sof tsql code to run if code =&#8217;a&#8217;.</p>
<p>if code = &#8216;b&#8217;<br />
select @ from dept</p>
<p>if code = &#8216;c&#8217;<br />
select  * from add</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cute_boboi</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-122861</link>
		<dc:creator><![CDATA[cute_boboi]]></dc:creator>
		<pubDate>Fri, 11 Mar 2011 11:23:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-122861</guid>
		<description><![CDATA[Just to provide more info, this is what I want to achieve:


DECLARE @var VARCHAR(1000)
SELECT @var = CASE DATEPART(dw, GetDate())
WHEN 6 THEN &#039;&#039;&#039;AED&#039;&#039;,&#039;&#039;AFA&#039;&#039;,&#039;&#039;ALL&#039;&#039;&#039;
ELSE &#039;&#039;&#039;AED&#039;&#039;,&#039;&#039;AFA&#039;&#039;&#039;
END
PRINT @var

-- 1st SQL : does not return any result ???
SELECT * FROM [AdventureWorks].[Sales].[Currency] a
WHERE a.CurrencyCode IN (@var)

SELECT @var = &#039;SELECT * FROM [AdventureWorks].[Sales].[Currency] a
WHERE a.CurrencyCode IN (&#039;+@var+&#039;)&#039;

PRINT @var
-- 2nd SQL : This is the result I want if it is Friday.
EXEC (@var)


Since today is Friday, it will return as per the 2nd SQL:
CurrencyCode	Name	ModifiedDate
AED	Emirati Dirham	1998-06-01 00:00:00.000
AFA	Afghani	1998-06-01 00:00:00.000
ALL	Lek	1998-06-01 00:00:00.000

If today is not Friday, I&#039;m expecting this result:
CurrencyCode	Name	ModifiedDate
AED	Emirati Dirham	1998-06-01 00:00:00.000
AFA	Afghani	1998-06-01 00:00:00.000

Thanks again]]></description>
		<content:encoded><![CDATA[<p>Just to provide more info, this is what I want to achieve:</p>
<p>DECLARE @var VARCHAR(1000)<br />
SELECT @var = CASE DATEPART(dw, GetDate())<br />
WHEN 6 THEN &#8221;&#8217;AED&#8221;,&#8221;AFA&#8221;,&#8221;ALL&#8221;&#8217;<br />
ELSE &#8221;&#8217;AED&#8221;,&#8221;AFA&#8221;&#8217;<br />
END<br />
PRINT @var</p>
<p>&#8211; 1st SQL : does not return any result ???<br />
SELECT * FROM [AdventureWorks].[Sales].[Currency] a<br />
WHERE a.CurrencyCode IN (@var)</p>
<p>SELECT @var = &#8216;SELECT * FROM [AdventureWorks].[Sales].[Currency] a<br />
WHERE a.CurrencyCode IN (&#8216;+@var+&#8217;)&#8217;</p>
<p>PRINT @var<br />
&#8211; 2nd SQL : This is the result I want if it is Friday.<br />
EXEC (@var)</p>
<p>Since today is Friday, it will return as per the 2nd SQL:<br />
CurrencyCode	Name	ModifiedDate<br />
AED	Emirati Dirham	1998-06-01 00:00:00.000<br />
AFA	Afghani	1998-06-01 00:00:00.000<br />
ALL	Lek	1998-06-01 00:00:00.000</p>
<p>If today is not Friday, I&#8217;m expecting this result:<br />
CurrencyCode	Name	ModifiedDate<br />
AED	Emirati Dirham	1998-06-01 00:00:00.000<br />
AFA	Afghani	1998-06-01 00:00:00.000</p>
<p>Thanks again</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cute_boboi</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-122858</link>
		<dc:creator><![CDATA[cute_boboi]]></dc:creator>
		<pubDate>Fri, 11 Mar 2011 11:16:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-122858</guid>
		<description><![CDATA[Correction: 
Change a.Currency_Code to a.CurrencyCode]]></description>
		<content:encoded><![CDATA[<p>Correction:<br />
Change a.Currency_Code to a.CurrencyCode</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cute_boboi</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-122856</link>
		<dc:creator><![CDATA[cute_boboi]]></dc:creator>
		<pubDate>Fri, 11 Mar 2011 11:12:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-122856</guid>
		<description><![CDATA[Using MSSQL 2008 Adventureworks.

The following syntax is wrong, but is something I want to do.
i.e. 
If it is Friday today, select out all the data with Currency_Code IN (&#039;AED&#039;, &#039;AFA&#039;, &#039;ALL&#039;)
Else, select out Currency_Code IN (&#039;AED&#039;, &#039;AFA&#039;)

SELECT * FROM [AdventureWorks].[Sales].[Currency] a
WHERE a.Currency_Code IN
	CASE DATEPART(dw, GetDate())
		WHEN 6 THEN &#039;AED&#039;, &#039;AFA&#039;, &#039;ALL&#039;     -- Friday
		ELSE &#039;AED&#039;, &#039;AFA&#039;
	END
	
Please with the SQL statement. Thanks]]></description>
		<content:encoded><![CDATA[<p>Using MSSQL 2008 Adventureworks.</p>
<p>The following syntax is wrong, but is something I want to do.<br />
i.e.<br />
If it is Friday today, select out all the data with Currency_Code IN (&#8216;AED&#8217;, &#8216;AFA&#8217;, &#8216;ALL&#8217;)<br />
Else, select out Currency_Code IN (&#8216;AED&#8217;, &#8216;AFA&#8217;)</p>
<p>SELECT * FROM [AdventureWorks].[Sales].[Currency] a<br />
WHERE a.Currency_Code IN<br />
	CASE DATEPART(dw, GetDate())<br />
		WHEN 6 THEN &#8216;AED&#8217;, &#8216;AFA&#8217;, &#8216;ALL&#8217;     &#8212; Friday<br />
		ELSE &#8216;AED&#8217;, &#8216;AFA&#8217;<br />
	END</p>
<p>Please with the SQL statement. Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-117025</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Mon, 07 Feb 2011 10:50:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-117025</guid>
		<description><![CDATA[You need to post some sample data with expected result]]></description>
		<content:encoded><![CDATA[<p>You need to post some sample data with expected result</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amit</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-116145</link>
		<dc:creator><![CDATA[Amit]]></dc:creator>
		<pubDate>Sat, 05 Feb 2011 05:06:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-116145</guid>
		<description><![CDATA[Could you guys help with this one?

In the case statement below i an getting only data for the first two case statement, and the last 3 statements are suppressed.

But when i put the second case statement in the end(i.e &#039;Total FEW&#039;) i am getting value for the first four condition and only the &#039;Total FEW&#039; condition is suppressed.

Pls give a solution!

Case 
When T2.A = &#039;CANADA&#039; and T2.B in (&#039;TRANSACTION FRAUD&#039;, &#039;IDENTITY FRAUD&#039;, &#039;INVESTIGATIONS&#039;) Then &#039;Canada&#039;
When T2.B  &#039;OTHER&#039; Then &#039;Total FEW&#039;
When T2.A in (&#039;BANK CARD EAST&#039;, &#039;BANK CARD WEST&#039;, &#039;CANADA&#039;, &#039;CHOICE&#039;, &#039;DEBIT&#039;, &#039;DEBIT EAST&#039;, &#039;DEBIT WEST&#039;, &#039;OTHER&#039;) and T2.B in (&#039;SMITH BARNEY&#039;, &#039;SPECIALTY&#039;, &#039;BOARDROOM&#039;, &#039;DOSS&#039;) Then &#039;DOSS&#039;
When T2.A in (&#039;BANK CARD EAST&#039;, &#039;BANK CARD WEST&#039;, &#039;CHOICE&#039;, &#039;CANADA&#039;, &#039;OTHER&#039;) and T2.B = &#039;IDENTITY FRAUD&#039; Then &#039;ID Fraud&#039;
When T2.A in (&#039;BANK CARD EAST&#039;, &#039;BANK CARD WEST&#039;, &#039;CHOICE&#039;, &#039;OTHER&#039;) and T2.B = &#039;TRANSACTION FRAUD&#039; Then &#039;Trans Fraud&#039;
End Department,]]></description>
		<content:encoded><![CDATA[<p>Could you guys help with this one?</p>
<p>In the case statement below i an getting only data for the first two case statement, and the last 3 statements are suppressed.</p>
<p>But when i put the second case statement in the end(i.e &#8216;Total FEW&#8217;) i am getting value for the first four condition and only the &#8216;Total FEW&#8217; condition is suppressed.</p>
<p>Pls give a solution!</p>
<p>Case<br />
When T2.A = &#8216;CANADA&#8217; and T2.B in (&#8216;TRANSACTION FRAUD&#8217;, &#8216;IDENTITY FRAUD&#8217;, &#8216;INVESTIGATIONS&#8217;) Then &#8216;Canada&#8217;<br />
When T2.B  &#8216;OTHER&#8217; Then &#8216;Total FEW&#8217;<br />
When T2.A in (&#8216;BANK CARD EAST&#8217;, &#8216;BANK CARD WEST&#8217;, &#8216;CANADA&#8217;, &#8216;CHOICE&#8217;, &#8216;DEBIT&#8217;, &#8216;DEBIT EAST&#8217;, &#8216;DEBIT WEST&#8217;, &#8216;OTHER&#8217;) and T2.B in (&#8216;SMITH BARNEY&#8217;, &#8216;SPECIALTY&#8217;, &#8216;BOARDROOM&#8217;, &#8216;DOSS&#8217;) Then &#8216;DOSS&#8217;<br />
When T2.A in (&#8216;BANK CARD EAST&#8217;, &#8216;BANK CARD WEST&#8217;, &#8216;CHOICE&#8217;, &#8216;CANADA&#8217;, &#8216;OTHER&#8217;) and T2.B = &#8216;IDENTITY FRAUD&#8217; Then &#8216;ID Fraud&#8217;<br />
When T2.A in (&#8216;BANK CARD EAST&#8217;, &#8216;BANK CARD WEST&#8217;, &#8216;CHOICE&#8217;, &#8216;OTHER&#8217;) and T2.B = &#8216;TRANSACTION FRAUD&#8217; Then &#8216;Trans Fraud&#8217;<br />
End Department,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-102252</link>
		<dc:creator><![CDATA[Madhivanan]]></dc:creator>
		<pubDate>Fri, 26 Nov 2010 06:59:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-102252</guid>
		<description><![CDATA[You need to post what the problem with your query]]></description>
		<content:encoded><![CDATA[<p>You need to post what the problem with your query</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emy</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-101849</link>
		<dc:creator><![CDATA[Emy]]></dc:creator>
		<pubDate>Wed, 24 Nov 2010 11:22:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-101849</guid>
		<description><![CDATA[Hi pinal.
could u please help mi on this query.

SELECT  e.Name +&#039; &#039; +  e.Surname as Name  
,count ([ID_EmployeeTask]) as workinkDays 
,sum ([FinalWT]) as PeriodTotalWT 
,sum ([FinalPoints]) as PeriodTotalpoints  
,sum ([FinalWT])/count ([ID_EmployeeTask]) as AverageWT
,sum ([FinalPoints])/count ([ID_EmployeeTask]) as ProductivityPerPeriod 
,&#039; ProductivityperHour&#039; =(case  when sum ([FinalWT])= 0 then 0 else sum ([FinalPoints])/sum ([FinalWT])End)
 FROM [OPS_ReportingDB].[Organization].employee e 
inner join [OPS_ReportingDB].[TaskData].[EmployeeTask] 
on [OPS_ReportingDB].[TaskData].[EmployeeTask].[ID_Employee] =  e.[ID_Employee] 
where (e.id_Employee in --(select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit inner join Organization.Department D on  u.ID_Department= D.ID_Department inner join Organization.Division I on D.ID_Division =I.ID_Division  and ID_HeadOfDivision = 2 ))
(case  
when 52 in (select ID_TeamLeader  from [OPS_ReportingDB].Organization.Unit)  
then (select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit and ID_TeamLeader = 52)  
when  52  in (select ID_HeadOfDepartment  from [OPS_ReportingDB].Organization.Department) 
then (select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit inner join Organization.Department D on  u.ID_Department= D.ID_Department and ID_HeadOfDepartment = 52 )   
when  52  in (select ID_HeadOfDivision  from [OPS_ReportingDB].Organization.Division) 
then  (select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit inner join Organization.Department D on  u.ID_Department= D.ID_Department inner join Organization.Division I on D.ID_Division =I.ID_Division  and ID_HeadOfDivision = 52 )
else (0) end) )
group by e.[ID_Employee] ,e.Name , e.Surname order by Name



regards]]></description>
		<content:encoded><![CDATA[<p>Hi pinal.<br />
could u please help mi on this query.</p>
<p>SELECT  e.Name +&#8217; &#8216; +  e.Surname as Name<br />
,count ([ID_EmployeeTask]) as workinkDays<br />
,sum ([FinalWT]) as PeriodTotalWT<br />
,sum ([FinalPoints]) as PeriodTotalpoints<br />
,sum ([FinalWT])/count ([ID_EmployeeTask]) as AverageWT<br />
,sum ([FinalPoints])/count ([ID_EmployeeTask]) as ProductivityPerPeriod<br />
,&#8217; ProductivityperHour&#8217; =(case  when sum ([FinalWT])= 0 then 0 else sum ([FinalPoints])/sum ([FinalWT])End)<br />
 FROM [OPS_ReportingDB].[Organization].employee e<br />
inner join [OPS_ReportingDB].[TaskData].[EmployeeTask]<br />
on [OPS_ReportingDB].[TaskData].[EmployeeTask].[ID_Employee] =  e.[ID_Employee]<br />
where (e.id_Employee in &#8211;(select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit inner join Organization.Department D on  u.ID_Department= D.ID_Department inner join Organization.Division I on D.ID_Division =I.ID_Division  and ID_HeadOfDivision = 2 ))<br />
(case<br />
when 52 in (select ID_TeamLeader  from [OPS_ReportingDB].Organization.Unit)<br />
then (select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit and ID_TeamLeader = 52)<br />
when  52  in (select ID_HeadOfDepartment  from [OPS_ReportingDB].Organization.Department)<br />
then (select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit inner join Organization.Department D on  u.ID_Department= D.ID_Department and ID_HeadOfDepartment = 52 )<br />
when  52  in (select ID_HeadOfDivision  from [OPS_ReportingDB].Organization.Division)<br />
then  (select e.id_Employee from Organization.Employee e inner join Organization.Unit u on e.ID_Unit =u.ID_Unit inner join Organization.Department D on  u.ID_Department= D.ID_Department inner join Organization.Division I on D.ID_Division =I.ID_Division  and ID_HeadOfDivision = 52 )<br />
else (0) end) )<br />
group by e.[ID_Employee] ,e.Name , e.Surname order by Name</p>
<p>regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kris</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-88046</link>
		<dc:creator><![CDATA[Kris]]></dc:creator>
		<pubDate>Thu, 16 Sep 2010 17:53:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-88046</guid>
		<description><![CDATA[Hi,

Would you be able to help me in an SQL query? The process is currently performing the following query.

SELECT A,B,C,D
FROM TableA
WHERE Date_First_Invoiced_AR NOT BETWEEN MM/DD/YY AND MM/DD/YY

I need to change it so that The Date_First_Invoiced_AR is from TableB, if it exists, and if not then use TableA.

I tried the following, but it did not work.

SELECT A,B,C,D,
CASE WHEN TableB.Date_First_Invoiced  &#039;&#039; THEN TableB.Date_First_Invoiced_AR ELSE TableA.Date_First_Invoiced_AR END AS DateInvoiced 
FROM TableA
LEFT OUTER JOIN TableB ON TableA.ID = TableB.ID
WHERE DateInvoiced NOT BETWEEN MM/DD/YY AND MM/DD/YY

I get an error message &#039;Invalid column name &#039;DateInvoiced&#039;.

Any help would be GREATLY appreciated!!]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Would you be able to help me in an SQL query? The process is currently performing the following query.</p>
<p>SELECT A,B,C,D<br />
FROM TableA<br />
WHERE Date_First_Invoiced_AR NOT BETWEEN MM/DD/YY AND MM/DD/YY</p>
<p>I need to change it so that The Date_First_Invoiced_AR is from TableB, if it exists, and if not then use TableA.</p>
<p>I tried the following, but it did not work.</p>
<p>SELECT A,B,C,D,<br />
CASE WHEN TableB.Date_First_Invoiced  &#8221; THEN TableB.Date_First_Invoiced_AR ELSE TableA.Date_First_Invoiced_AR END AS DateInvoiced<br />
FROM TableA<br />
LEFT OUTER JOIN TableB ON TableA.ID = TableB.ID<br />
WHERE DateInvoiced NOT BETWEEN MM/DD/YY AND MM/DD/YY</p>
<p>I get an error message &#8216;Invalid column name &#8216;DateInvoiced&#8217;.</p>
<p>Any help would be GREATLY appreciated!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86736</link>
		<dc:creator><![CDATA[Madhivanan]]></dc:creator>
		<pubDate>Mon, 06 Sep 2010 07:23:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86736</guid>
		<description><![CDATA[Post the code you used]]></description>
		<content:encoded><![CDATA[<p>Post the code you used</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: avinash</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86735</link>
		<dc:creator><![CDATA[avinash]]></dc:creator>
		<pubDate>Mon, 06 Sep 2010 07:17:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86735</guid>
		<description><![CDATA[i mtrying to convert the nvarchar datatype in to numeric.
but it showing error.

&quot;Error converting data type nvarchar to numeric.&quot; 
tell me how to convert this.]]></description>
		<content:encoded><![CDATA[<p>i mtrying to convert the nvarchar datatype in to numeric.<br />
but it showing error.</p>
<p>&#8220;Error converting data type nvarchar to numeric.&#8221;<br />
tell me how to convert this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86333</link>
		<dc:creator><![CDATA[Madhivanan]]></dc:creator>
		<pubDate>Thu, 02 Sep 2010 15:10:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86333</guid>
		<description><![CDATA[Try this

Select *
From Table as a
Where
(Code Not in (1,2,3,4) and Date &gt; ’10-02-2010′ )
OR
(Date &lt;- ’10-02-2010′ and Code not in(5,6,7,8))]]></description>
		<content:encoded><![CDATA[<p>Try this</p>
<p>Select *<br />
From Table as a<br />
Where<br />
(Code Not in (1,2,3,4) and Date &gt; ’10-02-2010′ )<br />
OR<br />
(Date &lt;- ’10-02-2010′ and Code not in(5,6,7,8))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich Westhaver</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86316</link>
		<dc:creator><![CDATA[Rich Westhaver]]></dc:creator>
		<pubDate>Thu, 02 Sep 2010 13:46:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-86316</guid>
		<description><![CDATA[I&#039;m trying to create a case statement in the Where Clause, but I keep getting an eror in the statement.
What I&#039;m trying to do is Run it so that if it is pulling after a specific date to filter out sppecific Codes:
Select *
From Table as a 
Where
Code = Case when Date &gt; &#039;10-02-2010&#039; then Code Not in (1,2,3,4) Else Code not in(5,6,7,8) End.  

I keep getting the following error:
Incorrect syntax near the keyword &#039;in&#039;.

Any thoughts?]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m trying to create a case statement in the Where Clause, but I keep getting an eror in the statement.<br />
What I&#8217;m trying to do is Run it so that if it is pulling after a specific date to filter out sppecific Codes:<br />
Select *<br />
From Table as a<br />
Where<br />
Code = Case when Date &gt; &#8217;10-02-2010&#8242; then Code Not in (1,2,3,4) Else Code not in(5,6,7,8) End.  </p>
<p>I keep getting the following error:<br />
Incorrect syntax near the keyword &#8216;in&#8217;.</p>
<p>Any thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chintan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-85980</link>
		<dc:creator><![CDATA[Chintan]]></dc:creator>
		<pubDate>Wed, 01 Sep 2010 03:59:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-85980</guid>
		<description><![CDATA[I have a scenario where I want to assign the value of column c2 to a seperate variable based on the value of column c1 in the same query. So if the value of c1 is 1, then the value of c2 should go be assigned to v1, if the value of c1 is 2, then the value of c2 should be assigned to variable v2 and so on. I tried the below query, but it gives a syntax error. Any help is appreciated.

SELECT CASE WHEN CURRENCY_CODE = &#039;USD&#039; THEN @USD_sec_alias = SECURITY_ALIAS
	    WHEN CURRENCY_CODE = &#039;GBP&#039; THEN @EUR_sec_alias = SECURITY_ALIAS
 	    WHEN CURRENCY_CODE = &#039;EUR&#039; THEN @EUR_sec_alias = SECURITY_ALIAS
       END
FROM   SECURITY.DBO.SECURITY_MASTER
WHERE  CURRENCY_CODE IN (&#039;USD&#039;,&#039;GBP&#039;,&#039;EUR&#039;)
AND    INVESTMENT_TYPE = &#039;CASH&#039;

Thank you,
Chintan Somaiya]]></description>
		<content:encoded><![CDATA[<p>I have a scenario where I want to assign the value of column c2 to a seperate variable based on the value of column c1 in the same query. So if the value of c1 is 1, then the value of c2 should go be assigned to v1, if the value of c1 is 2, then the value of c2 should be assigned to variable v2 and so on. I tried the below query, but it gives a syntax error. Any help is appreciated.</p>
<p>SELECT CASE WHEN CURRENCY_CODE = &#8216;USD&#8217; THEN @USD_sec_alias = SECURITY_ALIAS<br />
	    WHEN CURRENCY_CODE = &#8216;GBP&#8217; THEN @EUR_sec_alias = SECURITY_ALIAS<br />
 	    WHEN CURRENCY_CODE = &#8216;EUR&#8217; THEN @EUR_sec_alias = SECURITY_ALIAS<br />
       END<br />
FROM   SECURITY.DBO.SECURITY_MASTER<br />
WHERE  CURRENCY_CODE IN (&#8216;USD&#8217;,'GBP&#8217;,'EUR&#8217;)<br />
AND    INVESTMENT_TYPE = &#8216;CASH&#8217;</p>
<p>Thank you,<br />
Chintan Somaiya</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-85187</link>
		<dc:creator><![CDATA[Madhivanan]]></dc:creator>
		<pubDate>Wed, 25 Aug 2010 07:06:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-85187</guid>
		<description><![CDATA[Refer this post to know more examples about the CASE expression
http://beyondrelational.com/blogs/madhivanan/archive/2010/08/24/case-expression.aspx]]></description>
		<content:encoded><![CDATA[<p>Refer this post to know more examples about the CASE expression<br />
<a href="http://beyondrelational.com/blogs/madhivanan/archive/2010/08/24/case-expression.aspx" rel="nofollow">http://beyondrelational.com/blogs/madhivanan/archive/2010/08/24/case-expression.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Libin</title>
		<link>http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-85138</link>
		<dc:creator><![CDATA[Libin]]></dc:creator>
		<pubDate>Tue, 24 Aug 2010 17:55:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/#comment-85138</guid>
		<description><![CDATA[Nice, but  explanation is too short and not describe complex queries.

 need to explain some complex queries using case.]]></description>
		<content:encoded><![CDATA[<p>Nice, but  explanation is too short and not describe complex queries.</p>
<p> need to explain some complex queries using case.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
