How to Work With Case Statement in Transact-SQL

In this article I am going to explain about how to work with case statement in Transact-SQL.
  • 2420

Introduction

Transact-SQL (T-SQL) is Structured Query Language and it is Microsoft's and Sybase's proprietary. Transact-SQL is originally developed by IBM for querying, altering and defining relational database. Transact SQL is also known as T-SQL. T-SQL supports many feature like variables, loops, conditions  and exceptions.  

T-SQL provide the fallowing feature

  • Stored Function
  • Stored procedure
  • Trigger
  • Index
  • View

T-SQL provide the programming concept. It also provide a wide variety of data types for variables and constants. SQL provide limited support for advance programming and T-SQL fill this gap.  

Case statement

Case statement in T-SQL is the most basic of all the control flow statements. Case statement execute a sequence of statement depending upon condition. CASE statement in T SQL operates by comparing the first expression to the expression in each WHEN clause for equivalency. Expressions which  are equivalent in Case Statement,  expression in the THEN clause will be executed.

Example

declare

@st_marks int,

@st_result varchar(15)

select @st_marks = 80

select @st_result =

case

when @st_marks>70 then 'A Grade'

when @st_marks<70 and @st_marks>=50 then 'B Grade'

when @st_marks < 50 then 'Fail'

end

print 'result = ' + @st_result


© 2020 DotNetHeaven. All rights reserved.