Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

CASE Statement in SQL Server: What You Need to Know

Understand CASE Statement in SQL Server with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and...

Table of Contents

CASE Statement in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

This guide will show you in detail how to use the CASE statement handling function in SQL Server with specific syntax and examples to better visualize and capture functions.

Describe

CASE statement used to set branch conditions in SQL Server, similar to the function of IF-THEN-ELSE statement.

CASE has 2 formats:

  • Simple CASE function, also called Simple CASE.
  • The search function CASE is also called Searched CASE.

Inside:

  • Simple CASE is to compare an expression with a set of simple expressions to determine the result.
  • Searched CASE is to evaluate a set of Boolean expressions to determine results.
  • Both formats support the ELSE argument (but not required).

Syntax

To use CASE statements in SQL Server, we use the following syntax:

Simple CASE

 CASE bieuthuc_dauvao 
  WHEN bieuthuc_1 THEN ketqua_1 
  WHEN bieuthuc_2 THEN ketqua_2 
  . 
  WHEN bieuthuc_n THEN ketqua_n 
 ELSE ketqua_khac 
 END 

Or Searched CASE

 CASE 
  WHEN dieukien_1 THEN ketqua_1 
  WHEN dieukien_2 THEN ketqua_2 
  . 
  WHEN dieukien_n THEN ketqua_n 
 ELSE ketqua_khac 
 END 

Parameters :

  • bieuthuc_dauvao: The expression will be compared to each value provided.
  • bieuthuc_1, bieuthuc_2, bieuthuc_n: expressions will be used to compare in turn with the input expression. When an expression matches bieuthu_dauvao, CASE will execute the next statement and no further comparison.
  • dieukien_1, dieukien_2, dieukien_n : conditions reviewed, approved in the order listed. When a condition is determined to be true, CASE returns the result and no further conditions are evaluated. All conditions must be the same data type.
  • ketqua_1, ketqua_2, ketqua_n : the result returned after considering the condition is true. All values must be the same data type.

Note :

  • If no match is found for TRUE, the CASE statement will return the result in the ELSE clause.
  • If there is no ELSE clause and there is no TRUE condition then the CASE statement will return NULL.
  • Conditions are evaluated in the order listed. When a condition is determined to be true, the CASE statement returns the result and no further conditions are evaluated.
  • CASE can be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005.

For instance,

Take a look and explore some examples of CASE statements in SQL Server.

Example 1: Simple CASE

Suppose you need to check the section of the TipsMake.com website, based on the Code column of the conversion table:

  • If Code = '01' is 'Laptrinh-TipsMake.com'.
  • If Code = '02' is 'Congnghe-TipsMake.com'.
  • If Code = '03' is 'Cuocsong-TipsMake.com'.
  • On the contrary, that is not from 1 to 3 then in the section 'Khoahoc-TipsMake.com'.
 SELECT tenchuyenmuc, Code 
  (CASE code 
  WHEN 01 THEN 'Laptrinh-TipsMake.com' 
  WHEN 02 THEN 'Congnghe-TipsMake.com' 
  WHEN 03 THEN 'Cuocsong-TipsMake.com' 
  ELSE 'Khoahoc-TipsMake.com' 
  END) AS Chuyenmuc 
 FROM chuyenmuc 
 ORDER BY Code 

Results returned:

tenchuyenmuc Code Chuyenmuc SQL Server 01 Laptrinh-TipsMake.com Linux 02 Congnghe-TipsMake.com Python 01 Laptrinh-TipsMake.com JavaScript 01 Laptrinh-TipsMake.com Android 02 Congnghe-TipsMake.com Giai tri 03 Cuocsong-TipsMake.com Cuocsong-TipsMake.com Science Curve 04 Khoahoc-TipsMake.com Kham pha Science 05 Khoahoc-TipsMake.com

Example 2: Searched CASE

 SELECT tenchuyenmuc, 
  CASE 
  WHEN code = 01 THEN 'Laptrinh-TipsMake.com' 
  WHEN code = 02 THEN 'Congnghe-TipsMake.com' 
  WHEN code = 03 THEN 'Cuocsong-TipsMake.com' 
  ELSE 'Khoahoc-TipsMake.com' 
  END 
 FROM chuyenmuc; 

You may also not use ELSE conditions like this:

 SELECT tenchuyenmuc, Code 
  (CASE code 
  WHEN 01 THEN 'Laptrinh-TipsMake.com' 
  WHEN 02 THEN 'Congnghe-TipsMake.com' 
  WHEN 03 THEN 'Cuocsong-TipsMake.com' 
  END) AS Chuyenmuc 
 FROM chuyenmuc 
 ORDER BY Code 

Or:

 SELECT tenchuyenmuc, 
  CASE 
  WHEN code = 01 THEN 'Laptrinh-TipsMake.com' 
  WHEN code = 02 THEN 'Congnghe-TipsMake.com' 
  WHEN code = 03 THEN 'Cuocsong-TipsMake.com' 
  END 
 FROM chuyenmuc; 

When the ELSE clause is omitted, if no condition is true, the CASE statement returns NULL.

Example 3: Comparing two conditions

Here is an example that illustrates how to use CASE statements to compare different conditions:

 SELECT 
  CASE 
  WHEN code < 2 THEN 'Laptrinh-TipsMake.com' 
  WHEN code = 2 THEN 'Congnghe-TipsMake.com' 
  END 
 FROM chuyenmuc; 

Next lesson: COALESCE command in SQL Server

FAQ

What should you know about describe?

CASE statement used to set branch conditions in SQL Server, similar to the function of IF-THEN-ELSE statement.

What should you know about syntax?

To use CASE statements in SQL Server, we use the following syntax:

What should you know about for instance,?

Take a look and explore some examples of CASE statements in SQL Server.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.