Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my CASE expression returning an error?

Using the AdventureWorks file I typed the following query:

USE AdventureWorks2019

SELECT *
CASE 
    WHEN FirstName='Ken' THEN 1
ELSE 3
FROM Person.Person

But I keep getting the following error:

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword ‘CASE’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any ideas as to why this is?

>Solution :

You need a comma between the columns, and the CASE expression needs an "END" to terminate it, like so:

USE AdventureWorks2019

SELECT *,
    CASE 
        WHEN FirstName='Ken' THEN 1
        ELSE 3
    END as CaseColumn
FROM Person.Person

The as CaseColumn gives that column a specific name, which is optional but generally a good practice.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading