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’.
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.