Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Case And If Else Statement In Sql Server

DZone's Guide to

Case And If Else Statement In Sql Server

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.



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



It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:
database ,sql server

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}