Can any one tell me the how to use select case in sql? If there are multiple values in based on condition then where to put select case and what are formats of CASE expression?
Can any one tell me the how to use select case in sql? If there are multiple values in based on condition then where to put select case and what are formats of CASE expression?
This the example of select case in sql :
Code:DECLARE @TestVal int SET @TestVal = 4 SELECT CASE @TestVal WHEN one THEN 'First' WHEN two THEN 'Second' WHEN three THEN 'Third' WHEN four THEN 'fourth' ELSE 'Other' END
The CASE expression has two formats:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
Select Case allows you to present data in various way. You can take numbers and return text values and express. You can take numbers and return text values and express.The SQL CASE statement has WHEN, THEN, and ELSE clauses along with an END terminator.
Syntax :Code:CASE [expression] WHEN [value | Boolean expression] THEN [return value] [ELSE [return value]] END
Bookmarks