Over a million developers have joined DZone.
Silver Partner

Case And If Else Statement In Sql Server

· Database Zone


DECLARE @MyVal INT

DECLARE @OUTPUTValues VARCHAR(200)
SET @MyVal = 1

SELECT @OUTPUTValues =
(
CASE  @MyVal
        WHEN 1 THEN 'test1'
        WHEN 2 THEN 'test2'
        WHEN 3 THEN 'test3'
        ELSE 'New'
END
)

PRINT @OUTPUTValues

SET @TestVal = 5

IF @MyVal= 1
SET @OUTPUTValues= '1'
ELSE IF @MyVal= 2
SET @OUTPUTValues= '2'
ELSE IF @MyVal= 3
SET @OUTPUTValues= '3'
ELSE
SET @OUTPUTValues= @MyVal

PRINT @OUTPUTValues



Topics:
database,sql server

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}