Why am I getting Msg 156 syntax error when using a CASE expression?

I am trying to run this simple query, however it doesn't even parse the statement

Msg 156, Level 15, State 1, Line 2 Incorrect syntax next to the 'Between' keyword.

Inquiry:

select  
    case DMProject.dbo.TypeFourTraining.Column2 
    when Column2 between 181 and 360 then '181-360' 
    when Column2 between 0 and 180 then '0-180' 
END as score 
from DMProject.dbo.TypeFourTraining 

The same does not work for syntax Column2 < 360.

I searched over the internet from msdn and some other sites, but I see that my syntax seems valid, that is, either the part I need to know, or something that I don’t see :(

Can anyone suggest a solution?

+3
source share
2 answers

You cannot mix CASE simpleand searched. Remove the field name DMProject.dbo.TypeFourTraining.Column2after CASE.

:

SELECT  CASE 
            WHEN Column2 between 181    AND 360 THEN '181-360' 
            WHEN Column2 between 0      AND 180 THEN '0-180' 
        END as score 
FROM    DMProject.dbo.TypeFourTraining 

CASE:

CASE: simple searched. Simple Searched .

CASE:

CASE input
    WHEN 1 THEN 'a'
    WHEN 2 THEN 'b'
    WHEN 3 THEN 'c'
    ELSE ''
END

CASE :

CASE 
    WHEN input = 1 THEN 'a'
    WHEN input = 2 THEN 'b'
    WHEN input = 3 THEN 'c'
    ELSE ''
END

CASE :

. WHEN.

CASE 
    WHEN input = 1 AND second_column = 2 THEN 'a'
    WHEN input = 2 AND third_column  = 3 THEN 'b'
    WHEN input = 3 AND (second_column = 4 OR third_column = 6) THEN 'c'
    ELSE ''
END
+4

case. :

select (case when Column2 between 181 and 360 then '181-360' 
             when Column2 between 0 and 180 then '0-180' 
        END) as score 
from DMProject.dbo.TypeFourTraining 

:

select  case DMProject.dbo.TypeFourTraining.Column2 
            when 1 then '1' 
            when 2 then '2'
            etc. etc. etc. 
            END as score 
from DMProject.dbo.TypeFourTraining 
+3

All Articles