I'm trying to bucket unit statuses. What am I doing wrong with my case statement? I'm new to SQL.
CASE WHEN [sStatus] LIKE '%Notice%'
THEN 'Notice'
ELSE
CASE WHEN [sStatus] LIKE '%Occupied%'
THEN 'Occupied'
ELSE
CASE WHEN [sStatus] LIKE '%Vacant%'
THEN 'Vacant'
ELSE [sStatus]
END as [Status]
Thank you!
Your case statements are missing ends. But, they don't need to be nested in the first place:
(CASE WHEN [sStatus] LIKE '%Notice%' THEN 'Notice'
WHEN [sStatus] LIKE '%Occupied%' THEN 'Occupied'
WHEN [sStatus] LIKE '%Vacant%' THEN 'Vacant'
ELSE [sStatus]
END) as [Status]
And, if you just want the first word, you don't need a case
at all:
SUBSTRING(sStatus, CHARINDEX(' ', sStatus + ' '), LEN(sStatus))
You are getting the error because you have 3 CASE statements and only one END.
However, there is no need to nest these CASE statements at all. You can simply do this:
CASE
WHEN [sStatus] LIKE '%Notice%'
THEN 'Notice'
WHEN [sStatus] LIKE '%Occupied%'
THEN 'Occupied'
WHEN [sStatus] LIKE '%Vacant%'
THEN 'Vacant'
ELSE [sStatus]
END as [Status]
CASE WHEN [sStatus] LIKE '%Notice%' THEN 'Notice'
WHEN [sStatus] LIKE '%Occupied%' THEN 'Occupied'
WHEN [sStatus] LIKE '%Vacant%' THEN 'Vacant'
ELSE [sStatus] END as Status
You don't need all those else
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.