Select Case syntax

Peter Weinwurm 0 Reputation points
2023-02-18T18:55:50.6366667+00:00

Hello SQL Server Expert,

The below code works, however, if I change it to > 130 I get the following error message.

Incorrect syntax near '>'.

SELECT Case EmployeeNumber WHEN 130 THEN 'Big' 
			WHEN 200 THEN 'Very Big'
			ELSE 'Small' END  
			FROM dbo.tblEmployee

If I use the long form method it works. I am curious why the > syntax does not work in the short form method.

SELECT CASE WHEN EmployeeNumber > 130 THEN 'Big' 
				WHEN EmployeeNumber > 200 THEN 'Very Big'
				ELSE 'Small' END  
				FROM dbo.tblEmployee

Thank you

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,492 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 119.9K Reputation points MVP
    2023-02-18T19:52:43.5+00:00

    Because it is not permitted.

    The normal form of CASE is

    CASE WHEN condition-1 THEN value-1
         WHEN condition-2 THEN value-2
         ...
         ELSE 
    END
    

    But there are a couple of shortcuts permitted. One is COALESCE(value-1, value-2, ..., value-N) which looks like a function, but in fact is a shortcut for

    CASE WHEN value-1 IS NOT NULL THEN value-1
         WHEN value-2 IS NOT NULL THEN value-2
         ...
        ELSE value
    END
    
    

    There is also a shortcut for this type of expression:

    CASE WHEN col = par-1 THEN value-1
         WHEN col = par-2 THEN value-2
         WHEN col = par-3 THEN value-3
         ...
         ELSE value-N
    END
    
    

    That is, the same column or variable is compared with the = operator to different values. For this type of CASE expression, you can use the shortcut in your first example. But it is exactly under these condition. As soon as something creeps in which deviates from this pattern, you cannot use this shortcut.

    0 comments No comments

  2. PercyTang-MSFT 12,511 Reputation points Microsoft External Staff
    2023-02-20T02:56:09.8733333+00:00

    Hi @Peter Weinwurm

    Because SQL Server does not recognize such syntactic forms.

    When you use a form like 'Case EmployeeNumber WHEN 130 THEN 'Big'', it defaults to output Big when the EmployeeNumber is 130.

    But when you want to put the '>' symbol in front of 130, the system can't recognize such a form. You can try, replace '>' with '=', '<' will get an error.

    User's image

    So you have to abandon this simple form and use the long form.

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.