---
title: "How to use  Case Expression in SQL Server."  
description: "How to use  Case Expression in SQL Server."  
author: "Anonymous User"  
published: 2015-11-08  
updated: 2019-04-11  
canonical: https://www.mindstick.com/forum/33580/how-to-use-case-expression-in-sql-server  
category: ".net"  
tags: ["sql server"]  
reading_time: 2 minutes  

---

# How to use  Case Expression in SQL Server.

i wnat to use Case [Expression](https://www.mindstick.com/articles/1861/sqlite-expressions) in sqlserver.please help me.

## Replies

### Reply by Anonymous User

Where **A CASE** is the special scalar expression or conditional statement in the **SQL** language which returns a single value based on the evaluation of a statement. The Case statements can be used in Select and Where clauses and even an Order By clause. The Case expression is mostly used in SQL stored procedures or as a formula for a particular column, which optimizes the SQL statements.

We'll start by walking through a simple case statement. The Case statement can be used in two forms in SQL Server:

- Case statement with a simple expression.
- Case statement with the comparison or searched expression.
- Case statement with the simple expression

A Simple Case statement checks only for equivalent values and cannot contain Boolean expressions. The Simple Case statement looks for the first expression in the list of all when the clause that matches statement_1 and evaluates the corresponding when clause. Whether there is no match, then the else clause is evaluated.

```
The general syntax of the Simple CASE expression is:
CASE expression
WHEN exp_1 THEN result_1
[WHEN exp_2 THEN result_2]
[..................]
[WHEN exp_n THEN result_n]
[ELSE expression]
END
```

### Reply by Anonymous User

Sometimes, you required to fetch or modify the records based on some conditions. In this case, you may use cursor or loop for modify your records. In this situation Case expression is best alternative for Cursor/looping and also provides better performance.

You can use CASE expressions anywhere in the SQL Query like CASE expressions can be used with in SELECT statement, WHERE clauses, Order by clause, HAVING clauses,Insert, UPDATE and DLETE statements.

This compares an expression to a set of simple expressions to find the result. This expression compares an expression to the expression in each WHEN clause for equivalency. If the expression with in the WHEN clause is matched, the expression in the THEN clause will be returned.

## Syntax:

```
   CASE expressionWHEN
expression1 THEN Result1
WHEN
expression2 THEN Result2
ELSE ResultN
END
Example :
select CustomerID,CustomerName,Address,City,PostalCode,Country,(case when CustomerID=1 then 'First Record'
when CustomerID=2 then 'Second Record' end) expcase    from Customer
```

```

```

\


---

Original Source: https://www.mindstick.com/forum/33580/how-to-use-case-expression-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
