Usecases of CASE expression

  • When there are complex conditions, where result is depending on multiple columns.
  • When we have a specific order of condition to get executed.
  • When we want to create a dummy table with transformed values for analysis purposes.
  • When we apply aggregation on different cases.

SQL Server CASE Expression

The CASE expression is used to show another column which can be evaluated based on the conditions depending on the existing columns. The CASE expression consists of WHEN and THEN statements. WHEN is used when the condition satisfies and THEN executes the operation or assignment of value of that column based on the evaluation of the expression. The CASE statement can contain multiple WHEN and THEN statements based on the conditions. The THEN can also contain the ELSE statement if the condition in THEN is not satisfied then the ELSE statement will be assigned.

Similar Reads

Types of CASE Expressions

Simple CASE Expression...

How to Use CASE Expression?

For performing the operations, We should have one table on which operations will be performed. If you don’t know How to Create a table in SQL Server then refer this....

CASE Expression for Aggregate Function:

Using case expression we can apply the aggregate functions like COUNT, SUM , AVG etc, for the rows belonging to that particular case in the table....

Usecases of CASE expression

When there are complex conditions, where result is depending on multiple columns....

Conclusion

We can use the CASE statement when we have multiple conditions and based on the condition we should assign a particular value to a column and also in the case where we have to aggregate on the basis of a particular condition, the CASE expression becomes very useful....