Incorrect syntax next to the keyword "case"

what's wrong with this code

CREATE FUNCTION [dbo].[ChangeRevision] (@oldRev tinyint)
RETURNS varchar(1) 
AS
begin

declare @newRev varchar(1)
DECLARE @newval int
set @newval=CAST (@oldRev as int)

case @newval
begin
when 0 then set @newRev='Z'
when 1 then set @newRev='A'
when 2 then set @newRev='B'
when 3 then set @newRev='C'

end
return @newRev;

END

I have the following error Invalid syntax next to the keyword "case".

Incorrect syntax next to the keyword "Return".

+4
source share
3 answers

This should work:

SET @newRev = (SELECT case @newval
    WHEN 0 THEN 'Z'
    WHEN 1 THEN 'A'
    WHEN 2 THEN 'B'
    WHEN 3 THEN 'C'
    END)
+8
source

keyword BEGINfor casemissing in tsql

select @newRev=case @newval
when 0 then 'Z'
when 1 then 'A'
when 2 then 'B'
when 3 then 'C'
end
+1
source

case begin, end

.

SET @newRev = (SELECT case @newval
    WHEN 0 THEN 'Z'
    WHEN 1 THEN 'A'
    WHEN 2 THEN 'B'
    WHEN 3 THEN 'C'
    END)

MSDN CASE

+1

All Articles