0

I am new to SQL Server and doing a tutorial, but cant seem to find the error. It's exactly as the tutorial suggests, but won't execute. See error below. Does certain functionality need to be turned on? I am using SQL Server 2008R2 Express. Does that matter?

use AdventureWorks
go

SELECT top 5 Name, GroupName
 CASE GroupName
    when 'Research and Development' then 'RD'
    when 'Sales and Marketing' then 'SM'
    else 'Other'
 END 
FROM HumanResources.department;
go

The error is...

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'case'.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

3

You forgot your comma. Columns need to be comma delimited and because of how the case statement is setup, it is considered a column. In your case the comma goes in between the GroupName column, and the Case statement I aliased as myCase

use AdventureWorks
go

SELECT top 5 Name, GroupName, 
    CASE GroupName
         when 'Research and Development' then 'RD'
         when 'Sales and Marketing' then 'SM'
         else 'Other'
 END as myCase
FROM HumanResources.department;
go
Elias
  • 2,602
  • 5
  • 28
  • 57