Click here to Skip to main content
15,867,686 members
Articles / Database Development / SQL Server / SQL Server 2008

A Simple Use of SQL CASE Expression

Rate me:
Please Sign up or sign in to vote.
4.66/5 (43 votes)
19 Aug 2009CPOL2 min read 877.8K   722   43   20
This article will give you an idea about how to use CASE expression in T-SQL or as a formula of a particular column.

Table of Contents

Introduction

This article will give you an idea about how to use CASE expression in T-SQL or as a formula of a particular column.

What is CASE Expression 

CASE is the special scalar expression in SQL language. CASE expression is widely used to facilitate determining / setting a new value from user input values. CASE expression can be used for various purposes which depends on the business logic.

CASE expression is mostly used in SQL stored procedure or as a formula for a particular column, which optimizes the SQL statements.

Syntax of CASE Expression 

SQL CASE expression is used as a type of IF-THEN-ELSE statement. It is similar to switch statement in recent programming languages such as C# and Java. The syntax of the CASE statement is simple as follows:

SQL
1.    CASE column_name  
2.      WHEN condition1 THEN result1  
3.      WHEN condition2 THEN result2  
4.      ...  
5.      ELSE result  
6.    END

Sample Example of CASE Statement 

SQL
DECLARE @intInput INT
SET @intInput = 2
SELECT CASE(@intInput) WHEN 1 THEN 'One' WHEN 2 THEN 'Two' _
		WHEN 3 THEN 'Three' ELSE 'Your message.' END 

Use of CASE Expression

The case expression can be used anywhere scalar expressions are allowed, including in WHERE and HAVING clauses of the select statement.

In this article, I would like to show the most commonly used case expression in:

  • Stored procedure 
  • Formula of a particular column 
  • View

Basic Use in a Stored Procedure

A simple example of using CASE in a stored procedure is given below:

SQL
-- =============================================
-- Author: Md. Marufuzzaman
-- Create date: 
-- Description:    A simple example of CASE expression.
-- =============================================
/*
DECLARE @varCountry VARCHAR(100)
EXEC spGetCountry 1, @varCountry OUTPUT
SELECT @varCountry
*/

ALTER PROCEDURE [dbo].[spGetCountry]
 @intCode        INT
,@varOutPut         VARCHAR(100) OUTPUT
AS
BEGIN
 
SELECT CASE(@intCode) WHEN 1 THEN 'Country_1'
              WHEN 2 THEN 'Country_2'
              WHEN 3 THEN 'Country_3'
              WHEN 4 THEN 'Country_4'
              WHEN 5 THEN 'Country_5'
              WHEN 6 THEN 'Country_6'
              WHEN 7 THEN 'Country_7'
              WHEN 8 THEN 'Country_8'
              WHEN 9 THEN 'Country_9'
              WHEN 10 THEN 'Country_10'         
                      ELSE 'Unknown' END  
      
END

Basic Use in a Table Column Formula

When we create a Table in design mode, SQL server provides us the properties of each column, where we can set various property values like a default value of a column, identity of a column, etc. Every column has a special property that is a custom formula, where you can set your own formula for data manipulation. Let’s take an example:

Our target is to write a formula for a column, and this formula is responsible for setting a new value for another column.

Image 1
Figure 1 - How we can set a formula for a particular column

A simple example of using CASE in a Table column formula is given below:

SQL
// SQL CASE statement
(case [Code] when (1) then 'Country_1' when (2) then 'Country_2' _
	when (3) then 'Country_3' when (4) then 'Country_4' when (5) _
	then 'Country_5' when (6) then 'Country_6' when (7) then 'Country_7' _
	when (8) then 'Country_8' when (9) then 'Country_9' when (10) _
	then 'Country_10' else 'Unknown' end)

When you insert / update a value at column “code”, the SQL server will fire the formula which is associated with the column “code” and finally set the value of that particular column.

Output
Figure 2 - How column "Country" sets value when column code value is inserted / updated

Basic Use in a View

There is nothing new to use CASE expression in a view object. As I mentioned before, CASE expression can be used anywhere scalar expressions are allowed, including in WHERE and HAVING clauses of the select statement.

Conclusion 

I hope that you will get an idea about how to use CASE expression. Enjoy!

History

  • 18th August 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
Questionmy opeinion Pin
Member 1336652617-Aug-17 20:07
Member 1336652617-Aug-17 20:07 
Questionmultiple columns Pin
Abhishek Jaiswall28-Aug-14 23:34
Abhishek Jaiswall28-Aug-14 23:34 
AnswerRe: multiple columns Pin
Md. Marufuzzaman29-Aug-14 23:05
professionalMd. Marufuzzaman29-Aug-14 23:05 
GeneralMy vote of 4 Pin
Pratik Bhuva16-Dec-13 1:55
professionalPratik Bhuva16-Dec-13 1:55 
GeneralRe: My vote of 4 Pin
Md. Marufuzzaman29-Aug-14 23:06
professionalMd. Marufuzzaman29-Aug-14 23:06 
Welcome

Regards,
Md. Marufuzzaman
GeneralMy vote of 5 Pin
karthik reddy mereddy10-Sep-13 22:41
karthik reddy mereddy10-Sep-13 22:41 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman29-Aug-14 23:07
professionalMd. Marufuzzaman29-Aug-14 23:07 
Questionhow to give Multiple expressions Pin
PhanindraTSK15-Jul-13 21:11
PhanindraTSK15-Jul-13 21:11 
AnswerRe: how to give Multiple expressions Pin
Md. Marufuzzaman15-Jul-13 23:30
professionalMd. Marufuzzaman15-Jul-13 23:30 
GeneralMy vote of 5 Pin
AndrewOkoth25-Apr-13 21:43
AndrewOkoth25-Apr-13 21:43 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman15-Jul-13 23:31
professionalMd. Marufuzzaman15-Jul-13 23:31 
GeneralMy vote of 5 Pin
Tasnia.Maruf20-Sep-10 0:26
Tasnia.Maruf20-Sep-10 0:26 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman3-Oct-10 4:13
professionalMd. Marufuzzaman3-Oct-10 4:13 
GeneralMy vote of 1 Pin
grandtree29-Apr-10 16:05
grandtree29-Apr-10 16:05 
GeneralCase syntax Pin
Member 150309625-Aug-09 21:27
Member 150309625-Aug-09 21:27 
GeneralRe: Case syntax Pin
Md. Marufuzzaman26-Aug-09 0:35
professionalMd. Marufuzzaman26-Aug-09 0:35 
GeneralRe: Case syntax Pin
Md. Marufuzzaman26-Aug-09 0:35
professionalMd. Marufuzzaman26-Aug-09 0:35 
GeneralRe: Case syntax Pin
Md. Marufuzzaman26-Aug-09 0:49
professionalMd. Marufuzzaman26-Aug-09 0:49 
GeneralYou are doing great ! Pin
Abhijit Jana19-Aug-09 5:36
professionalAbhijit Jana19-Aug-09 5:36 
GeneralRe: You are doing great ! Pin
Md. Marufuzzaman19-Aug-09 7:22
professionalMd. Marufuzzaman19-Aug-09 7:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.