0

Case Statement syntax in MySQL:-

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE

Case Statement syntax in SQL Server 2008:-

Simple CASE expression: 
CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 

Follow SQL SERVER CASE STATEMENT SYNTAX

I have explained the difference so that users can understand what I am tryng to say here

I have the following code in MySql:-

SELECT sum(case when Year=2014 or purchased=0 then 0 else TC.TOTAL_SP_COST-TC.TOTAL_CP_COST end) as EARNINGs

from TABLE TC

If you see properly the case statement has 2 fields (Year and purchased)

When i tried the same code in MS SQL SERVER 2008,i got a red line under "or" when i tried to add 2 field in case statement in SQL SERVER.

SELECT sum(case (Year or purchased) when 2014 then 0 when 0 then 0 else TC.TOTAL_SP_COST-TC.TOTAL_CP_COST end) as EARNINGs

So the simple question is "Can we add multiple fields in case statement in SQL SERVER ?" IF YES How can we do it ?

Shivam657
  • 733
  • 1
  • 8
  • 29
  • 3
    You're being downvoted for asking the question without putting any prior research into it, like an amateur. Just as an exercise in understanding the community here, hover your mouse over the upvote arrow next to any question. The tool tip clearly says "This question shows research effort; it is useful and clear." – Jonathan Van Matre Jan 17 '14 at 22:32
  • 2
    There are two correct answers I am confused as to why neither is accepted. – Zane Jan 17 '14 at 23:00
  • @Zane - There are 2 answers,I haven't tried any one of them in my code,will accept as soon as I get results. – Shivam657 Jan 18 '14 at 05:09

2 Answers2

5

Did you try anything at all? The MySQL syntax you've posted and the MSSQL CASE are completely identical.

Edit for Revision 2:

The issue with your queries here is primary that you are mixing up the two valid forms for CASE syntax. There are two, and you are using the first in MySQL and mixing the two in your MSSQL query:

CASE
    WHEN <expression>
     AND <expression>
      THEN <result>
END

or

CASE <input>
    WHEN <expression>
     AND <expression>
      THEN <result>
END

Edit 3: Never Trust a MySQL

As MySQL is known to allow lots of completely whacky things, I went to see if the mis-ported query would even run on MySQL. Surprisingly ( but not shockingly ), it does. However, it is worth noting that the original MySQL query:

SELECT SUM( CASE 
           WHEN ( Year = 2014 OR purchased = 0 )
             THEN 0
           ELSE TC.TOTAL_SP_COST - TC.TOTAL_CP_COST 
       END ) AS EARNINGs
FROM `TABLE` TC;

Has a meaning which is far different than the MySQL-compatible second query, presented here with the SUM omitted and a second column to illustrate how the expression is evaluated:

SELECT CASE ( Year OR purchased ) 
           WHEN 2014 
             THEN 0 -- This line is unreachable;  [BOOLEAN]: { , 0, 1 }
           WHEN 0 
             THEN 0 -- This line only possible when Year == 0;  Bad Year data;
           ELSE TC.TOTAL_SP_COST - TC.TOTAL_CP_COST
       END AS EARNINGs,
       ( Year OR purchased ) AS Res
FROM `TABLE` TC;
Community
  • 1
  • 1
Avarkx
  • 1,055
  • 9
  • 17
  • Seems like you answered abruptly.I have tried a lot of things and i sincerly feel that before answering questions you should try and revise your concepts. – Shivam657 Jan 17 '14 at 21:37
  • 2
    @Shivam657 I had no way of knowing you intended to revise the question 2 hours later. That said, once I have a few free moments, I will absolutely review your changes and alter my answer appropriately. – Avarkx Jan 17 '14 at 22:09
  • 2
    @Shivam657 I think it is you who should think before you ask. You're the one who wants help, right? – Aaron Bertrand Jan 17 '14 at 22:12
  • @AaronBertrand - The question was voted down because it was thought that the case statement has the same syntax both IN MSSQL and MySQL,whihc is not right.I do not know why it was interpreted as such and voted down even though it didn't deserve to have any vote down.And if people are not sure about the question they can comment and clarify their doubts before voting it down and answering – Shivam657 Jan 18 '14 at 04:47
  • @Avarkx - But still what voted the question down because you thought that case statements have same syntax in MSSQL and MySQL,so i hope now that's not the case,and question in clear,the vote down should be taken back. – Shivam657 Jan 18 '14 at 04:49
  • The syntax of the `CASE` expression **is** the same in both MySQL and SQL-Server (both versions of the syntax are valid in both DBMS.). – ypercubeᵀᴹ Jan 18 '14 at 11:29
  • @Shivam657 I removed my down vote when you edited the question to show effort. – Avarkx Jan 18 '14 at 15:34
  • @Avarkx - But seems like other just can't resist a vote down. – Shivam657 Jan 18 '14 at 17:20
  • @Avarkx,@ypercube,@Aaron Bertrand - I was confused that why I asked the question,the syntax of case statement is same.Mistake committed out of confusion,but thanks to you guys. – Shivam657 Jan 18 '14 at 17:21
5

The code you have in the question looks fine - whether you got a red squiggly line under the or or not. Did you even try to execute it, or did you assume that because there was a red line there, there's no possible way it would work? IntelliSense is anything but - often it is way behind or it complains about things elsewhere in your code that have nothing to do with the current statement. For example, do you think there's really a problem with the second statement here?

enter image description here

If I highlight that statement, the SELECT will still be underlined as if it is incorrect. That doesn't mean it will fail when I execute it.

Here is an example showing that your code works just fine, in spite of the red squiggly you're seeing - for probably a completely unrelated reason.

CREATE TABLE dbo.[TABLE]
(
  [Year] INT,
  purchased BIT,
  TOTAL_SP_COST INT,
  TOTAL_CP_COST INT
);

SELECT SUM(CASE WHEN [Year] = 2014 OR purchased = 0 THEN 0 
  ELSE TOTAL_SP_COST - TC.TOTAL_CP_COST END) AS EARNINGs
FROM dbo.[TABLE] AS TC;

Of course I made a couple of other adjustments, e.g. when you must use reserved words (even future keywords) as column names (you really should fix that), you should escape them properly, and you should always use the schema prefix and get in the habit of using semi-colons as statement terminators.

Meaningless SQL Fiddle example, since I have no idea of your sample data or desired results.

Finally, an important semantic thing: CASE is an expression, not a statement. The important thing you're missing from the syntax diagram is that the input and output of a searched CASE expression must itself be a single expression, and there are no boolean expressions in SQL Server. So you can't say:

CASE (col1 OR col2) WHEN something THEN ...
Community
  • 1
  • 1
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Thanks for the edit,will remember it for the future.Will try your code in the query.See if it works. – Shivam657 Jan 18 '14 at 05:07
  • Would remember in future not to put question without putting proper explaination and complete code.Thanks btw for your answer. – Shivam657 Jan 18 '14 at 06:29